How can I control the order of files in a ZIP archive?

14

7

There are times when it is necessary for the files in a ZIP archive to be in a specific order (for example, I'm currently creating EPUBs, which require that the first entry in the ZIP be the mimetype file). How can I do this on Windows?

I'm currently using 7-Zip as my archiver, but I don't see an option for controlling file order. Can this be done with 7-zip? If not, what options do I have?

Ben Blank

Posted 2012-02-03T00:20:09.393

Reputation: 824

No surprise that VMware products require the contents of an OVA file to be in a certain order. – pacoverflow – 2018-04-16T15:40:04.363

3Adding them in the order that they need to be should do this. Try zipping the mimetype file first, then adding the other files to the zip as a second operation. – Paul – 2012-02-03T00:25:49.177

1@Paul — Surprisingly, that didn't work. :-/ – Ben Blank – 2012-02-03T01:01:18.460

Is it LIFO maybe? Can you try adding mimetype last? You are testing against an epub reader right, rather than just unzipping? – Paul – 2012-02-03T01:08:16.943

3A application that requires the contents to be in a specific order is broken. – psusi – 2012-02-03T03:52:24.913

@psusi — I don't disagree, but that's what's in the spec. – Ben Blank – 2012-02-03T05:43:29.040

3Sheesh, the authors of the spec need taken out back and beaten with a clue-by-four. – psusi – 2012-02-03T14:31:41.847

Be aware that the order of the files in the archive is not necessarily the order of the files in the directory, and the directory order is what you'll see (if even that) if you use an unzip tool to "peek" at the directory. The only way to tell the real order is with a hex editor, or a specially-written program. – Daniel R Hicks – 2013-02-06T23:51:52.967

(Adding in order -- as separate steps, not simply listing the files in order in one operation -- should put them in the archive in that order. The only way for this not to happen would be if the archive tool, for some strange reason, decided to rewrite the archive rather than just append to it.) – Daniel R Hicks – 2013-02-06T23:53:30.797

Answers

5

7-zip GUI version:

  1. extract mimetype from EPUB file
  2. delete mimetype in EPUB file
  3. rename mimetype to !mimetype
  4. add !mimetype to EPUB file
  5. rename !mimetype to mimetype inside EPUB file

7-zip simple drag and drop version:

Go to 7-zip download page and download 7-Zip Extra: standalone console version, 7z DLL, Plugin for Far Manager (doesn't work with stable 9.20, but works well with 9.38 beta and 15.06 beta). Extract 7za.exe. Save code below as repair.bat file:

7za x %1 mimetype -y
7za d %1 mimetype
move /y mimetype !mimetype
7za a %1 !mimetype
del /f !mimetype
7za rn %1 !mimetype mimetype

Put 7za.exe and repair.bat in the same directory and simply drag your EPUB file on repair.bat.

Tithen-Firion

Posted 2012-02-03T00:20:09.393

Reputation: 297

3

One way would be: Most archivers, and archive tools have the ability to append or add additional file items to the original archive. Create your archive with the items you want at the top , then add additional files items to that created archive. I know this works in windows own ZIP tools.

looks like the real answer is beyond me , here for C# https://stackoverflow.com/questions/5898787/creating-an-epub-file-with-a-zip-library

here for HTML (using 7z)
http://next.blurb.com/2011/02/17/how-to-make-an-ipad-photo-book/

specificly this part

2.Open the sample-photo-book folder, and copy the file mimetype into the (empty) archive first.
3.Now, copy the other folders (META-INF and OEBPS) into the archive.


http://idpf.org/epub/30/spec/epub30-ocf.html#physical-container-zip

The contents of the mimetype file must not contain any leading padding or whitespace, must not begin with the Unicode signature (or Byte Order Mark), and the case of the MIME type string must be exactly as presented above. The mimetype file additionally must be neither compressed nor encrypted, and there must not be an extra field in its ZIP header.

Psycogeek

Posted 2012-02-03T00:20:09.393

Reputation: 8 067

In my experience recreating the compressed EPUB archive manually can cause problems with the EPUB being readable in Adobe Digital Editions e.g. with font embedding or the file being readable at all. – Ad Astm – 2017-06-15T12:33:16.427

1I gave this a try in 7-zip (creating an archive with only mimetype, then adding the other files), but had no success. The mimetype data came first in the file, but the file table seems to be alphabetized. – Ben Blank – 2012-02-03T01:04:19.297

2@BenBlank Make sure you aren't checking this using a zip client - they will re-order in a way that suits humans, not necessarily what an epub reader will see. – Paul – 2012-02-03T01:10:03.510

Presumably the problem is that 7-Zip likes to maintain it's directory in alpha order and then (necessarily) rewrites it when it closes the zip file. One could, in theory, fix this by editing the zip file with a hex editor or some sort of ad-hoc tool and rearranging the directory -- it's not particularly complicated. Editing would be simplified by including a dummy file that always sorts first. But then, of course, the technique in the above link should work too. – Daniel R Hicks – 2012-02-03T01:22:07.967

I used 7z l filename.zip to list the contents of a zip, and they don't appear to be in any order (name, path, date, size...). So, using this command line may help determine the true order of the files in the zip. – Scott McClenning – 2012-02-03T01:26:48.213

@Paul — I'm checking by opening the .epub in a text editor and examining the file table directly, so it looks like this approach simply isn't working for me. I'm going to take the Python hammer to the problem and see if that doesn't fix it. – Ben Blank – 2012-02-03T02:35:23.433

@BenBlank - The directory is rewritten every time you append to a zip, and is not necessarily in any order (though file order would be normal). The displayed order may of course be different, if the display tool decides to alphabetize. You'd need a hex editor to tell what the real order is. – Daniel R Hicks – 2013-02-06T23:55:26.937

2

Creating an EPUB requires a two step process, first add mimetype without any (!) compression, second add the other files/folders. So using 7z you could take the following commands:

7z a -mx=0 dummy.zip mimetype

To be able to use the wildcard * you should move the file mimetype to another folder and start the second step:

7z a -r dummy.zip *

After that rename the ZIP to EPUB and you're gone!

Peter

Posted 2012-02-03T00:20:09.393

Reputation: 29

I have just tried this and I still get this error: `Epubcheck Version 3.0.1

ERROR: C:/MYSELF/Books/1984---George-Orwell/1984---George-Orwell.epub: Mimetype entry missing or not the first in archive` – Tomáš Zato - Reinstate Monica – 2014-10-17T22:24:35.023

1

In my experience, the simplest answer (for Windows, 7, 64-bit) is to use Window's built-in Zip functionality. Use the "Send To" to send the mimetype to a new zipped folder, then use 7Zip to add the META-INF and OEBPS to the new zip. That seems to always work in terms of sequencing the mimetype file first. I have not had success performing this operation solely with 7-zip. (This is for ePUBS, but it should work, for sequencing, in other areas as well.)

I hope this helps someone else.

Kimberly Hitchens

Posted 2012-02-03T00:20:09.393

Reputation: 11

0

I had this same problem when working with EPUB-files.

On Windows 7 I fixed it with this strange trick where i made sure that I sorted all of the files that i was going to make in to a zip in a way that made mimetype the file on top (sorting them by size usually). Then I made sure that i marked all of the files from top to bottom with my cursor before sending them to a compressed (zip) folder (like this: https://support.microsoft.com/en-us/help/14200/windows-compress-uncompress-zip-files). After that I could change the name and filetype to whatever.epub.

However, after I upgraded to Windows 10 this trick did not work anymore. Luckily I have found another way. First you send the file you want at the top (mimetype) in the zip file to a compressed(zip) folder. Make sure it is only that file. Than I take the other files i want to zip (two folder in my case) mark them with the cursor and drag them up to the zip file i just created. Windows will then add the new files to the zip file, and keep the file you originally ziped on top.

MatsKS

Posted 2012-02-03T00:20:09.393

Reputation: 1

1Can you clearly indicate what the answer to the author's question is? Upgrading to Windows 10 is not really an answer to this question. You should concentrate on how you fixed it, provide specifics, your statements are way to broad. If I were to reproduce the original problem, I would be unable to solve it with your answer. – Ramhound – 2016-10-19T20:54:49.657

0

Using winrar did the trick for me. I added the mimetype to a zip-archive (uncompressed!), then later added the remaining folders, regular compression.

This solution did not work with 7-zip for me, as it keeps reordering files.

kat

Posted 2012-02-03T00:20:09.393

Reputation: 3