Skip to content

Commit 58abd8f

Browse files
committed
BAEL-3641 fix for creating directories inside a zip and handling missing entry for root folder in windows-created archives
1 parent bd3f54e commit 58abd8f

1 file changed

Lines changed: 46 additions & 35 deletions

File tree

  • core-java-modules/core-java-io/src/main/java/com/baeldung/unzip

core-java-modules/core-java-io/src/main/java/com/baeldung/unzip/UnzipFile.java

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,50 @@
88
import java.util.zip.ZipInputStream;
99

1010
public class UnzipFile {
11-
public static void main(final String[] args) throws IOException {
12-
final String fileZip = "src/main/resources/unzipTest/compressed.zip";
13-
final File destDir = new File("src/main/resources/unzipTest");
14-
final byte[] buffer = new byte[1024];
15-
final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
16-
ZipEntry zipEntry = zis.getNextEntry();
17-
while (zipEntry != null) {
18-
final File newFile = newFile(destDir, zipEntry);
19-
final FileOutputStream fos = new FileOutputStream(newFile);
20-
int len;
21-
while ((len = zis.read(buffer)) > 0) {
22-
fos.write(buffer, 0, len);
23-
}
24-
fos.close();
25-
zipEntry = zis.getNextEntry();
26-
}
27-
zis.closeEntry();
28-
zis.close();
29-
}
30-
31-
/**
32-
* @see https://snyk.io/research/zip-slip-vulnerability
33-
*/
34-
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
35-
File destFile = new File(destinationDir, zipEntry.getName());
36-
37-
String destDirPath = destinationDir.getCanonicalPath();
38-
String destFilePath = destFile.getCanonicalPath();
39-
40-
if (!destFilePath.startsWith(destDirPath + File.separator)) {
41-
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
42-
}
43-
44-
return destFile;
45-
}
11+
public static void main(final String[] args) throws IOException {
12+
final String fileZip = "src/main/resources/unzipTest/compressed.zip";
13+
final File destDir = new File("src/main/resources/unzipTest");
14+
final byte[] buffer = new byte[1024];
15+
final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
16+
ZipEntry zipEntry = zis.getNextEntry();
17+
while (zipEntry != null) {
18+
final File newFile = newFile(destDir, zipEntry);
19+
if (zipEntry.isDirectory()) {
20+
if (!newFile.isDirectory() && !newFile.mkdirs()) {
21+
throw new IOException("Failed to create directory " + newFile);
22+
}
23+
} else {
24+
File parent = newFile.getParentFile();
25+
if (!parent.isDirectory() && !parent.mkdirs()) {
26+
throw new IOException("Failed to create directory " + parent);
27+
}
28+
29+
final FileOutputStream fos = new FileOutputStream(newFile);
30+
int len;
31+
while ((len = zis.read(buffer)) > 0) {
32+
fos.write(buffer, 0, len);
33+
}
34+
fos.close();
35+
}
36+
zipEntry = zis.getNextEntry();
37+
}
38+
zis.closeEntry();
39+
zis.close();
40+
}
41+
42+
/**
43+
* @see https://snyk.io/research/zip-slip-vulnerability
44+
*/
45+
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
46+
File destFile = new File(destinationDir, zipEntry.getName());
47+
48+
String destDirPath = destinationDir.getCanonicalPath();
49+
String destFilePath = destFile.getCanonicalPath();
50+
51+
if (!destFilePath.startsWith(destDirPath + File.separator)) {
52+
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
53+
}
54+
55+
return destFile;
56+
}
4657
}

0 commit comments

Comments
 (0)