Skip to content

Commit

Permalink
iluwatar#4: Unzip an archive
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilbarar committed Aug 28, 2018
1 parent 6d538d4 commit b868c70
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/*
Expand Down Expand Up @@ -312,4 +313,28 @@ public static int httpGet(URL address) throws IOException {
HttpURLConnection con = (HttpURLConnection) address.openConnection();
return con.getResponseCode();
}

/**
* UnZip an archive
* @param zipFilename the file name of the archived file
* @throws IOException
*/
public static void unzipArchive(String zipFileName) throws IOException {
byte[] buffer = new byte[1024];
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFileName))) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
String fileName = zipEntry.getName();
File newFile = new File(fileName);
try (FileOutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
}
}
}
15 changes: 15 additions & 0 deletions src/test/java/LibraryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,19 @@ public void testHttpGet() throws IOException {
int responseCode = Library.httpGet(new URL("http://www.google.com"));
assertEquals(200, responseCode);
}

/**
* Tests for {@link Library#unzipArchive(String)}
*/
@Test
public void testUnzipArchive() throws IOException {
final String archivedFile = "src/test/resources/somearchivedfile.zip";
final String unarchivedFile = "somearchivedfile.txt";
try {
Library.unzipArchive(archivedFile);
assertTrue(Files.exists(Paths.get(unarchivedFile)));
} finally {
Files.deleteIfExists(new File(unarchivedFile).toPath());
}
}
}

0 comments on commit b868c70

Please sign in to comment.