@@ -123,14 +123,13 @@ public static String readFileToString(String filePath) throws IOException {
123123 }
124124 }
125125
126- private static String validateFileName (String fileName , String targetDirectory ) throws IOException {
127- File file = new File (fileName );
128- String canonicalPath = file .getCanonicalPath ();
126+ private static String validateFileName (String fileName , File destinationFolder ) throws IOException {
127+ String destinationFolderCanonicalPath = destinationFolder .getCanonicalPath ();
129128
130- File targetFile = new File (targetDirectory );
131- String targetCanonicalPath = targetFile .getCanonicalPath ();
129+ File file = new File (destinationFolderCanonicalPath , fileName );
130+ String canonicalPath = file .getCanonicalPath ();
132131
133- if (!canonicalPath .startsWith (targetCanonicalPath )) {
132+ if (!canonicalPath .startsWith (destinationFolderCanonicalPath )) {
134133 throw new IllegalStateException ("File is outside extraction target directory." );
135134 }
136135
@@ -151,12 +150,12 @@ public static void unzipFile(File zipFile, String destination) throws IOExceptio
151150 if (destinationFolder .exists ()) {
152151 deleteFileOrFolderSilently (destinationFolder );
153152 }
154-
153+
155154 destinationFolder .mkdirs ();
156155
157156 byte [] buffer = new byte [WRITE_BUFFER_SIZE ];
158157 while ((entry = zipStream .getNextEntry ()) != null ) {
159- String fileName = validateFileName (entry .getName (), "." );
158+ String fileName = validateFileName (entry .getName (), destinationFolder );
160159 File file = new File (destinationFolder , fileName );
161160 if (entry .isDirectory ()) {
162161 file .mkdirs ();
0 commit comments