zeroturnaround / zt-zip

ZeroTurnaround ZIP Library
http://www.zeroturnaround.com/
Apache License 2.0
1.38k stars 252 forks source link

Failure when directory in zip contains colon #117

Closed FWDekker closed 5 years ago

FWDekker commented 5 years ago

When a directory in a zip has a colon (:) in its name, zt-zip will throw a ZipException when unzipping the archive. This is likely because colons are not allowed in paths on Windows.

Other unzipping tools work around this issue by replacing the colon with an underscore (e.g. 7zip) or two underscores (e.g. Windows' native unzipping tool). I would expect zt-zip to behave similarly.

Note that files in a zip with a colon in the name are not a problem for zt-zip; it will simply truncate the filename starting from the colon.

Sample setup

Zip

The following zip will trigger the exception when it is unpacked: zip.zip

Code

I used the following code to unpack the zip:

package com.fwdekker.test;

import org.zeroturnaround.zip.ZipUtil;

import java.io.File;

public final class MyClass {
    public static void main(String[] args) {
        ZipUtil.unpack(MyClass.class.getResourceAsStream("/zip.zip"), new File("output/"));
    }
}

Exception

The following exception was thrown:

Exception in thread "main" org.zeroturnaround.zip.ZipException: Failed to process zip entry '2019-02-04T14:38/ with action org.zeroturnaround.zip.ZipUtil$Unpacker@72ea2f77
    at org.zeroturnaround.zip.ZipUtil.iterate(ZipUtil.java:715)
    at org.zeroturnaround.zip.ZipUtil.unpack(ZipUtil.java:1096)
    at org.zeroturnaround.zip.ZipUtil.unpack(ZipUtil.java:1045)
    at com.fwdekker.test.MyClass.main(MyClass.java:10)
Caused by: java.io.IOException: Unable to create directory output\2019-02-04T14:38
    at org.zeroturnaround.zip.commons.FileUtilsV2_2.forceMkdir(FileUtilsV2_2.java:898)
    at org.zeroturnaround.zip.ZipUtil$Unpacker.process(ZipUtil.java:1163)
    at org.zeroturnaround.zip.ZipUtil.iterate(ZipUtil.java:712)
    ... 3 more
toomasr commented 5 years ago

I ran a quick test on a Mac and Ubuntu. The unzip command leaves the colon intact. Is this a Windows only issue?

Would the NameMapper help you? See the examples here https://github.com/zeroturnaround/zt-zip#extract-a-directory-from-a-zip-archive-including-the-directory-name

FWDekker commented 5 years ago

It is indeed a Windows-only issue since Windows doesn't allow colons in filenames. But the NameMapper was able to solve the issue. Thanks!