BAEL-3641 fix for creating directories inside a zip and handling missing entry for root folder in windows-created archives
This commit is contained in:
parent
683215b2fe
commit
ebf99bfd4f
@ -8,39 +8,50 @@ import java.util.zip.ZipEntry;
|
|||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
public class UnzipFile {
|
public class UnzipFile {
|
||||||
public static void main(final String[] args) throws IOException {
|
public static void main(final String[] args) throws IOException {
|
||||||
final String fileZip = "src/main/resources/unzipTest/compressed.zip";
|
final String fileZip = "src/main/resources/unzipTest/compressed.zip";
|
||||||
final File destDir = new File("src/main/resources/unzipTest");
|
final File destDir = new File("src/main/resources/unzipTest");
|
||||||
final byte[] buffer = new byte[1024];
|
final byte[] buffer = new byte[1024];
|
||||||
final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
|
final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
|
||||||
ZipEntry zipEntry = zis.getNextEntry();
|
ZipEntry zipEntry = zis.getNextEntry();
|
||||||
while (zipEntry != null) {
|
while (zipEntry != null) {
|
||||||
final File newFile = newFile(destDir, zipEntry);
|
final File newFile = newFile(destDir, zipEntry);
|
||||||
final FileOutputStream fos = new FileOutputStream(newFile);
|
if (zipEntry.isDirectory()) {
|
||||||
int len;
|
if (!newFile.isDirectory() && !newFile.mkdirs()) {
|
||||||
while ((len = zis.read(buffer)) > 0) {
|
throw new IOException("Failed to create directory " + newFile);
|
||||||
fos.write(buffer, 0, len);
|
}
|
||||||
}
|
} else {
|
||||||
fos.close();
|
File parent = newFile.getParentFile();
|
||||||
zipEntry = zis.getNextEntry();
|
if (!parent.isDirectory() && !parent.mkdirs()) {
|
||||||
}
|
throw new IOException("Failed to create directory " + parent);
|
||||||
zis.closeEntry();
|
}
|
||||||
zis.close();
|
|
||||||
}
|
final FileOutputStream fos = new FileOutputStream(newFile);
|
||||||
|
int len;
|
||||||
/**
|
while ((len = zis.read(buffer)) > 0) {
|
||||||
* @see https://snyk.io/research/zip-slip-vulnerability
|
fos.write(buffer, 0, len);
|
||||||
*/
|
}
|
||||||
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
|
fos.close();
|
||||||
File destFile = new File(destinationDir, zipEntry.getName());
|
}
|
||||||
|
zipEntry = zis.getNextEntry();
|
||||||
String destDirPath = destinationDir.getCanonicalPath();
|
}
|
||||||
String destFilePath = destFile.getCanonicalPath();
|
zis.closeEntry();
|
||||||
|
zis.close();
|
||||||
if (!destFilePath.startsWith(destDirPath + File.separator)) {
|
}
|
||||||
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
|
|
||||||
}
|
/**
|
||||||
|
* @see https://snyk.io/research/zip-slip-vulnerability
|
||||||
return destFile;
|
*/
|
||||||
}
|
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
|
||||||
|
File destFile = new File(destinationDir, zipEntry.getName());
|
||||||
|
|
||||||
|
String destDirPath = destinationDir.getCanonicalPath();
|
||||||
|
String destFilePath = destFile.getCanonicalPath();
|
||||||
|
|
||||||
|
if (!destFilePath.startsWith(destDirPath + File.separator)) {
|
||||||
|
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
return destFile;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user