File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 34
34
import java .util .*;
35
35
import java .util .List ;
36
36
import java .util .zip .ZipEntry ;
37
+ import java .util .zip .ZipInputStream ;
37
38
import java .util .zip .ZipOutputStream ;
38
39
39
40
/*
@@ -312,4 +313,28 @@ public static int httpGet(URL address) throws IOException {
312
313
HttpURLConnection con = (HttpURLConnection ) address .openConnection ();
313
314
return con .getResponseCode ();
314
315
}
316
+
317
+ /**
318
+ * UnZip an archive
319
+ * @param zipFilename the file name of the archived file
320
+ * @throws IOException
321
+ */
322
+ public static void unzipArchive (String zipFileName ) throws IOException {
323
+ byte [] buffer = new byte [1024 ];
324
+ try (ZipInputStream zis = new ZipInputStream (new FileInputStream (zipFileName ))) {
325
+ ZipEntry zipEntry = zis .getNextEntry ();
326
+ while (zipEntry != null ) {
327
+ String fileName = zipEntry .getName ();
328
+ File newFile = new File (fileName );
329
+ try (FileOutputStream fos = new FileOutputStream (newFile )) {
330
+ int len ;
331
+ while ((len = zis .read (buffer )) > 0 ) {
332
+ fos .write (buffer , 0 , len );
333
+ }
334
+ }
335
+ zipEntry = zis .getNextEntry ();
336
+ }
337
+ zis .closeEntry ();
338
+ }
339
+ }
315
340
}
Original file line number Diff line number Diff line change @@ -278,4 +278,19 @@ public void testHttpGet() throws IOException {
278
278
int responseCode = Library .httpGet (new URL ("http://www.google.com" ));
279
279
assertEquals (200 , responseCode );
280
280
}
281
+
282
+ /**
283
+ * Tests for {@link Library#unzipArchive(String)}
284
+ */
285
+ @ Test
286
+ public void testUnzipArchive () throws IOException {
287
+ final String archivedFile = "src/test/resources/somearchivedfile.zip" ;
288
+ final String unarchivedFile = "somearchivedfile.txt" ;
289
+ try {
290
+ Library .unzipArchive (archivedFile );
291
+ assertTrue (Files .exists (Paths .get (unarchivedFile )));
292
+ } finally {
293
+ Files .deleteIfExists (new File (unarchivedFile ).toPath ());
294
+ }
295
+ }
281
296
}
You can’t perform that action at this time.
0 commit comments