How to create a zip file compatible with Windows under Linux

49

10

I need to make a zip file available to all my Windows users visitors, so I naively produced a zip file with the Unix zip command (let's call it madeinlinux.zip).

It opens successfully with WinRar or Winzip, but those of my users who are using the standard Windows zip file handling experience failure when trying to unzip it. (Windows XP)

I compressed the same data using Windows built-in zip mecanism, and from a Linux point of view, I cannot see any difference in the file type:

$ file madeinlinux.zip :  Zip archive data, at least v2.0 to extract
$ file madeinwindows.zip : Zip archive data, at least v2.0 to extract

They're must be something specific to a Windows compatible zip file.

Does anyone knows what?

user3213

Posted 2009-07-10T10:40:00.977

Reputation:

I end up using format different from zip (like 7zip, rar, etc) — so that users wouldn't try opening that with the buggy built-in unzip. – Hi-Angel – 2015-11-03T06:32:01.177

1Could you produce one of these ZIP files (with dummy content) and put it on a server for us to download and inspect? – Bernhard Hofmann – 2009-07-10T10:45:07.690

This sounds like a case for superuser.com, if it exists yet. – None – 2009-07-10T10:45:13.353

1

Sure bernhard, here's the culprit: http://www.careerjet.co.uk/devel/Services_Careerjet.zip

– None – 2009-07-10T10:51:29.783

The only windows machine I had to test was a Windows 7 one, and that had no problems opening and extracting the file using explorer. – None – 2009-07-10T10:55:51.053

hail windows 7 ! – None – 2009-07-10T11:13:23.343

use gzip. afaik .. it has no problem with windows extractor. – None – 2009-07-10T12:06:39.630

Answers

28

Try with:

zip -9 -y -r -q file.zip folder/
  • -9 Indicates the slowest compression speed (optimal compression, ignores the suffix list)
  • -y Store symbolic links as such in the zip archive, instead of compressing and storing the file referred to by the link
  • -r Travel the directory structure recursively
  • -q Quiet mode

Igor Fobia

Posted 2009-07-10T10:40:00.977

Reputation: 380

Why would this help with XP compatibility? – Wowfunhappy – 2019-05-27T15:26:41.280

Honestly it was so much time ago that I don't remember clearly; but I can imagine that following the symbolic links could lead to problems (that could happen without -r) and -r allows you take all the folder content – Igor Fobia – 2019-06-26T09:06:13.723

11

7zip is an open source compression tool that works on Linux, FreeBSD, Mac OS X, BeOS, DOS, Amiga and Windows.

I would highly recommend it based on the windows version.

It supports

packing / unpacking: 7z, ZIP, GZIP, BZIP2 and TAR

Unpacking only: ARJ, CAB, CHM, CPIO, DEB, DMG, HFS, ISO, LZH, LZMA, MSI, NSIS, RAR, RPM, UDF, WIM, XAR and Z.

Bruce McLeod

Posted 2009-07-10T10:40:00.977

Reputation: 5 490

8I recommend not using yet another 3rd party proprietary tool for this unbelievably common utility (zip a file) available now on all platforms. – Rick O'Shea – 2017-03-20T17:55:10.350

8

Only thing that looks relevant is this

-k - Attempt  to  convert  the  names  and paths to conform to MSDOS, store only the MSDOS attribute (just the user write attribute from UNIX), and mark the entry as made under
MSDOS (even though it was not); for compatibility with PKUNZIP under MSDOS which cannot handle certain names such as those with two dots.

but do read "man zip" on your system before going anywhere else...

Dan Rosenstark

Posted 2009-07-10T10:40:00.977

Reputation: 5 718

1Hi. Thx for the suggestion, but this -k option takes me back in time a bit too much. It transforms all file name in a 8 character/no case version :( – None – 2009-07-10T11:41:38.760

Yeah, I remember those days. But did it help the file to read by the built-in Zip program on Windows? – None – 2009-07-10T13:43:59.317

Don't know. This file name issue stopped me trying – None – 2009-07-10T15:55:01.650

My guess is that it's the compression, as MSalters says... – None – 2009-07-10T19:20:50.550

8

zip -Z sets the compression option. -Z store is the most trivial one, as it doesn't compress at all. This is useful when you're using zip as an alternative for tar, or when troubleshooting. In this case you should try to see if an uncompressed archive is usable from Windows. If that is usable, you know that you'll have to pick a non-default compression option.

MSalters

Posted 2009-07-10T10:40:00.977

Reputation: 7 587

awesome, I'd guess it's the compression algorithm that is causing trouble too... – None – 2009-07-10T13:44:51.140

4

In addition to what others suggested, it's important pay Attention to your file and directory names as Windows does not necessarily like Linux file path and names. It sometimes also escapes them differently when zipping. Examples are numerous, but most importantly dot files (. and ..), files with only case differences (name.txt and NAME.txt), absolute file paths (/tmp/file.txt). Some other characters which are allowed in file names on Windows could cause issues when Windows Explorer is used to open files. In my case ':' character was the deal breaker but took a lot of work to find this out.

So before you resume to using using a lot of parameters, I suggest follow a simple procedure:

  1. Locate the folder or file your zipping up.

  2. run: zip -9 -r -k zip-modified-names.zip /path/to/your/folder

  3. pay attention to what the console spits out. In my case ':' in file names were stripped out.
  4. Move the zip file to a windows machine and attempt to open it.

If this works, you may be better off removing the characters that have been stripped off by -k option from your file/directory names try zipping normally. Note some parameters such as -k have side effects. In this case -k contradicts with -q option (for sym links).

Also -k option may render your file names unreadable. In my case my files were named based on creation time (e.g. 10:55:39.pdf) to facilitate easily locating the required record from archives, but -k option turned it to 105539.pdf which is not easily readable by users. I hence changed the names to 10_55_39.pdf which opens on Windows without using -k option but is still readable.

Shakus

Posted 2009-07-10T10:40:00.977

Reputation: 141

1@TD.512 have you noticed that the 6 years old question still doesn't have definite answer? Best to add another answer, if the answer seems to help someone as others didn't. – Hi-Angel – 2015-11-03T06:36:54.233

3

Had a similar issue recently with files produced from a perl script. Found that native windows zip (tested Windows 7 only) incorrectly handles paths with a leading slash and displays an empty zipfile. Solution was to strip the leading slash before adding files. Perhaps some versions of the linux zip command store file paths with leading slashes.

Nicholas Hardy

Posted 2009-07-10T10:40:00.977

Reputation: 31

2

According to the App. Notes on the pkware site ( http://www.pkware.com/support/zip-app-note/archives ): "The name of the file, with optional relative path. The path stored should not contain a drive or device letter, or a leading slash."

– EKW – 2013-04-07T04:39:51.870

2

Here is a python script that I am using to zip some files. It has been tested on ubuntu and Vista. A zip generated on Ubuntu opens with the Vista zipper.

I think that I had a similar issue in the past and it was because the zip format was not ZIP_DEFLATED. I am not sure. I will check that.

I hope it helps

import zipfile
import glob, os, sys

class ZipArchive:

    def zip_it(self, dirName, files):
        dirNamePrefix = dirName+"/*"
        for filename in glob.glob(dirNamePrefix):
            if os.path.isfile(filename) and (not self.exclude_svn or (filename.find(".svn\\")==-1)):
                print filename
                name = filename[len(self.folder)+1:]
                self.archive.write(filename, name, zipfile.ZIP_DEFLATED)

    def run(self, folder, name, exclude_svn):
        self.exclude_svn = exclude_svn
        self.folder = folder
        self.archive = zipfile.ZipFile(name+".zip", "w")
        os.path.walk(self.folder, ZipArchive.zip_it, self)
        self.archive.close()

if __name__ == "__main__":
    if (len(sys.argv)==1):
        print "usage zipit folder [name] [svn:yes|no]"
    else:
        name = sys.argv[1]
        exclude_svn = False

        if (len(sys.argv)>2): name = sys.argv[2]
        if (len(sys.argv)>3): exclude_svn = (sys.argv[3]=="no")

        arch = ZipArchive()
        arch.run(sys.argv[1], name, exclude_svn)
        print "done"

luc

Posted 2009-07-10T10:40:00.977

Reputation: 121

the question is, can it be unzipped using Windows zip mechanism? – None – 2009-07-10T12:07:40.923

yes. i've opened it with the Vista zip tool. I hope it works for you too – None – 2009-07-10T12:25:18.750

0

There is probably a problem in your file transference from Linux to Windows. If you´re using FTP, try setting a binary transfer (bin command in Windows, before the transference of your files from Linux to Windows).

Luis Andrés García

Posted 2009-07-10T10:40:00.977

Reputation: 121

Disagree. I've run into similar problems as the OP, particularly with old versions of Windows. I've done integrity checks and the shasum's match up. Plus, note that OP says the files decompress properly in 3rd party programs. – Wowfunhappy – 2019-05-27T15:32:06.493