FileUtil { public static void unzip(final File sourceZip, final File targetFolder) { FileInputStream fis = null; try { fis = new FileInputStream(sourceZip); final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); LOG.debug("Extracting zip file '" + sourceZip.getAbsolutePath() + "' to '" + targetFolder + "'"); unzip(zis, targetFolder); } catch (final Exception e) { throw new RuntimeException("unable to expand upgrade files for " + sourceZip + " to " + targetFolder, e); } finally { if (fis != null) { try { fis.close(); } catch (Exception ignore) { } } } }  static void deleteFolder(File folder); static void unzip(final File sourceZip, final File targetFolder); static void unzip(final Path sourceZip, final File targetFolder, final FileSystem fileSystem,
          final boolean localSpool); static void unzip(final ZipInputStream zis, final File targetFolder); static void zip(final File inputFolder, final File outputFile); static void unzipInDfs(FileSystem fileSystem, final Path source, final Path target); static final FilenameFilter VISIBLE_FILES_FILTER; }
@Test public void testUnzipFileFile() { File targetFolder = _temporaryFolder.newFolder("unpacked1"); FileUtil.unzip(TestResources.SHARD1, targetFolder); File segment = new File(targetFolder, "segments.gen"); assertTrue("Unzipped local zip directly to target", segment.exists()); }
@Test public void testUnzipPathFileFileSystemBoolean() throws IOException { Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.getLocal(configuration); File targetFolder = _temporaryFolder.newFolder("unpacked2"); Path zipPath = new Path(TestResources.SHARD1.getAbsolutePath()); FileUtil.unzip(zipPath, targetFolder, fileSystem, false); File segment = new File(targetFolder, "segments.gen"); assertTrue("Unzipped local zip directly to target", segment.exists()); targetFolder = _temporaryFolder.newFolder("unpacked3"); zipPath = new Path(TestResources.SHARD1.getAbsolutePath()); FileUtil.unzip(zipPath, targetFolder, fileSystem, true); segment = new File(targetFolder, "segments.gen"); assertTrue("Unzipped spooled local zip to target", segment.exists()); }
@Test public void testUnzipZipInputStreamFile() throws IOException { File targetFolder = _temporaryFolder.newFolder("unpacked4"); final Pipe zipPipe = Pipe.open(); final SinkChannel sink = zipPipe.sink(); final SourceChannel source = zipPipe.source(); final InputStream sourceIn = Channels.newInputStream(source); final OutputStream sourceOut = Channels.newOutputStream(sink); final ZipInputStream zis = new ZipInputStream(sourceIn); final FileInputStream fis = new FileInputStream(TestResources.SHARD1); final AtomicBoolean failed = new AtomicBoolean(false); Thread writer = new Thread() { @Override public void run() { try { int b; while ((b = fis.read()) >= 0) { sourceOut.write(b); } } catch (IOException e) { System.err.println("shard transfer via pipe failed: " + e); e.printStackTrace(System.err); failed.set(true); } finally { try { fis.close(); } catch (IOException ignore) { } try { sourceOut.close(); } catch (IOException ignore) { } } } }; writer.start(); FileUtil.unzip(zis, targetFolder); File segment = new File(targetFolder, "segments.gen"); assertTrue("Unzipped streamed zip to target", segment.exists()); }