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- 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- }
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+ }
2828
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- }
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+ }
4141
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 ());
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 ());
4747
48- String destDirPath = destinationDir .getCanonicalPath ();
49- String destFilePath = destFile .getCanonicalPath ();
48+ String destDirPath = destinationDir .getCanonicalPath ();
49+ String destFilePath = destFile .getCanonicalPath ();
5050
51- if (!destFilePath .startsWith (destDirPath + File .separator )) {
52- throw new IOException ("Entry is outside of the target dir: " + zipEntry .getName ());
53- }
51+ if (!destFilePath .startsWith (destDirPath + File .separator )) {
52+ throw new IOException ("Entry is outside of the target dir: " + zipEntry .getName ());
53+ }
5454
55- return destFile ;
56- }
55+ return destFile ;
56+ }
5757}
0 commit comments