88import java .util .zip .ZipInputStream ;
99
1010public 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