@@ -123,14 +123,13 @@ public static String readFileToString(String filePath) throws IOException {
123
123
}
124
124
}
125
125
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 ();
129
128
130
- File targetFile = new File (targetDirectory );
131
- String targetCanonicalPath = targetFile .getCanonicalPath ();
129
+ File file = new File (destinationFolderCanonicalPath , fileName );
130
+ String canonicalPath = file .getCanonicalPath ();
132
131
133
- if (!canonicalPath .startsWith (targetCanonicalPath )) {
132
+ if (!canonicalPath .startsWith (destinationFolderCanonicalPath )) {
134
133
throw new IllegalStateException ("File is outside extraction target directory." );
135
134
}
136
135
@@ -151,12 +150,12 @@ public static void unzipFile(File zipFile, String destination) throws IOExceptio
151
150
if (destinationFolder .exists ()) {
152
151
deleteFileOrFolderSilently (destinationFolder );
153
152
}
154
-
153
+
155
154
destinationFolder .mkdirs ();
156
155
157
156
byte [] buffer = new byte [WRITE_BUFFER_SIZE ];
158
157
while ((entry = zipStream .getNextEntry ()) != null ) {
159
- String fileName = validateFileName (entry .getName (), "." );
158
+ String fileName = validateFileName (entry .getName (), destinationFolder );
160
159
File file = new File (destinationFolder , fileName );
161
160
if (entry .isDirectory ()) {
162
161
file .mkdirs ();
0 commit comments