validatePath( Paths.get(destDirectory + File.separator + entry.getName()), destDirectory); if (entry.isDirectory()) { extractFile(zipIn, outputPath);
@Test public void testUnzipDirectoriesMissing() throws IOException { byte[] contents = "contents of file.txt".getBytes(); File directoriesMissing = _folder.newFile("directories_missing"); try (FileOutputStream fos = new FileOutputStream(directoriesMissing); ZipOutputStream out = new ZipOutputStream(fos)) { out.putNextEntry(new ZipEntry("missing_dir/child_dir/file.txt")); out.write(contents); } File dest = _folder.newFolder("dest"); UnzipUtility.unzip(directoriesMissing.toPath(), dest.toPath()); File file = dest.toPath().resolve("missing_dir").resolve("child_dir").resolve("file.txt").toFile(); assertThat("output file exists", file.exists(), is(true)); assertThat("output file is a file", file.isDirectory(), is(false)); assertThat("output file contents", Files.readAllBytes(file.toPath()), equalTo(contents)); }
@Test public void testUnzipEmptyFile() throws IOException { File empty = _folder.newFile("empty"); try (FileOutputStream fos = new FileOutputStream(empty); ZipOutputStream ignored = new ZipOutputStream(fos)) { assert ignored != null; // avoid unused warning. } File dest = _folder.newFolder("dest"); UnzipUtility.unzip(empty.toPath(), dest.toPath()); assertThat("output folder exists", dest.exists(), is(true)); assertThat("output folder is a directory", dest.isDirectory(), is(true)); assertThat("output folder is empty", dest.list(), equalTo(new String[0])); }
@Test public void testUnzipPathViolation() throws IOException { File pathViolation = _folder.newFile("pathViolation"); try (FileOutputStream fos = new FileOutputStream(pathViolation); ZipOutputStream out = new ZipOutputStream(fos)) { out.putNextEntry(new ZipEntry("../../")); out.putNextEntry(new ZipEntry("../../file.txt")); out.write("contents".getBytes()); } File dest = _folder.newFolder("dest"); _thrown.expect(BatfishException.class); _thrown.expectCause(instanceOf(IOException.class)); UnzipUtility.unzip(pathViolation.toPath(), dest.toPath()); } }
UnzipUtility.unzip(zipFile, unzipDir);
CommonUtil.writeStreamToFile(fileStream, snapshotZipFile); Path unzipDir = CommonUtil.createTempDirectory("tr"); UnzipUtility.unzip(snapshotZipFile, unzipDir);
@Test public void testLoadSnapshotInputObjectDirectory() throws IOException { NetworkId network = new NetworkId("network"); SnapshotId snapshot = new SnapshotId("snapshot"); String testSting = "this is life"; Path testdir = _storage.getSnapshotInputObjectPath(network, snapshot, "testkey"); testdir.toFile().mkdirs(); Files.write(testdir.resolve("testfile"), testSting.getBytes()); Path tmpzip = _folder.getRoot().toPath().resolve("tmp.zip"); try (InputStream inputStream = _storage.loadSnapshotInputObject(network, snapshot, "testkey")) { FileUtils.copyInputStreamToFile(inputStream, tmpzip.toFile()); } Path unzipDir = _folder.getRoot().toPath().resolve("tmp"); UnzipUtility.unzip(tmpzip, unzipDir); // the top level entry in the zip should be testkey String[] toplevel = unzipDir.toFile().list(); assertThat(toplevel, equalTo(new String[] {"testkey"})); // then, there should be testfile String[] secondlevel = unzipDir.resolve(toplevel[0]).toFile().list(); assertThat(secondlevel, equalTo(new String[] {"testfile"})); // the content of the testfile should match what we wrote assertThat( new String( Files.readAllBytes(unzipDir.resolve(toplevel[0]).resolve(secondlevel[0])), StandardCharsets.UTF_8), equalTo(testSting)); }