List of usage examples for org.apache.commons.compress.archivers ArchiveEntry isDirectory
public boolean isDirectory();
From source file:com.ttech.cordovabuild.infrastructure.archive.ArchiveUtils.java
private static void extractArchive(Path localPath, ArchiveInputStream ais) throws IOException { ArchiveEntry ae; while ((ae = ais.getNextEntry()) != null) { if (ae.isDirectory()) { continue; }// w w w . j ava 2 s . c o m Path filePath = localPath.resolve(ae.getName()); if (!filePath.getParent().equals(localPath)) Files.createDirectories(filePath.getParent()); try (OutputStream outputStream = Files.newOutputStream(filePath)) { IOUtils.copy(ais, outputStream); } } }
From source file:edu.unc.lib.dl.util.ZipFileUtil.java
/** * Unzip to the given directory, creating subdirectories as needed, and * ignoring empty directories. Uses apache zip tools until java utilies are * updated to support utf-8.//from www . j a v a2s . com */ public static void unzip(InputStream is, File destDir) throws IOException { BufferedInputStream bis = new BufferedInputStream(is); try (ZipArchiveInputStream zis = new ZipArchiveInputStream(bis)) { ArchiveEntry entry = null; while ((entry = zis.getNextZipEntry()) != null) { if (!entry.isDirectory()) { File f = new File(destDir, entry.getName()); if (!isFileInsideDirectory(f, destDir)) { throw new IOException( "Attempt to write to path outside of destination directory: " + entry.getName()); } f.getParentFile().mkdirs(); int count; byte data[] = new byte[8192]; // write the files to the disk try (FileOutputStream fos = new FileOutputStream(f); BufferedOutputStream dest = new BufferedOutputStream(fos, 8192)) { while ((count = zis.read(data, 0, 8192)) != -1) { dest.write(data, 0, count); } } } } } }
From source file:fr.gael.dhus.util.UnZip.java
public static void unCompress(String zip_file, String output_folder) throws IOException, CompressorException, ArchiveException { ArchiveInputStream ais = null;// w w w .j a v a 2 s . c om ArchiveStreamFactory asf = new ArchiveStreamFactory(); FileInputStream fis = new FileInputStream(new File(zip_file)); if (zip_file.toLowerCase().endsWith(".tar")) { ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis); } else if (zip_file.toLowerCase().endsWith(".zip")) { ais = asf.createArchiveInputStream(ArchiveStreamFactory.ZIP, fis); } else if (zip_file.toLowerCase().endsWith(".tgz") || zip_file.toLowerCase().endsWith(".tar.gz")) { CompressorInputStream cis = new CompressorStreamFactory() .createCompressorInputStream(CompressorStreamFactory.GZIP, fis); ais = asf.createArchiveInputStream(new BufferedInputStream(cis)); } else { try { fis.close(); } catch (IOException e) { LOGGER.warn("Cannot close FileInputStream:", e); } throw new IllegalArgumentException("Format not supported: " + zip_file); } File output_file = new File(output_folder); if (!output_file.exists()) output_file.mkdirs(); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = ais.getNextEntry()) != null) { File ftemp = new File(output_folder, nextEntry.getName()); if (nextEntry.isDirectory()) { ftemp.mkdir(); } else { FileOutputStream fos = FileUtils.openOutputStream(ftemp); IOUtils.copy(ais, fos); fos.close(); } } ais.close(); fis.close(); }
From source file:net.shopxx.util.CompressUtils.java
public static void extract(File srcFile, File destDir) { Assert.notNull(srcFile);/*w w w.j a v a 2 s . c o m*/ Assert.state(srcFile.exists()); Assert.state(srcFile.isFile()); Assert.notNull(destDir); Assert.state(!destDir.exists() || destDir.isDirectory()); destDir.mkdirs(); ArchiveInputStream archiveInputStream = null; try { archiveInputStream = new ArchiveStreamFactory() .createArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile))); ArchiveEntry archiveEntry; while ((archiveEntry = archiveInputStream.getNextEntry()) != null) { if (archiveEntry.isDirectory()) { new File(destDir, archiveEntry.getName()).mkdirs(); } else { OutputStream outputStream = null; try { outputStream = new BufferedOutputStream( new FileOutputStream(new File(destDir, archiveEntry.getName()))); IOUtils.copy(archiveInputStream, outputStream); } catch (FileNotFoundException e) { throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } finally { IOUtils.closeQuietly(outputStream); } } } } catch (FileNotFoundException e) { throw new RuntimeException(e.getMessage(), e); } catch (ArchiveException e) { throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } finally { IOUtils.closeQuietly(archiveInputStream); } }
From source file:com.audiveris.installer.Expander.java
/** * Untar an input file into an output directory. * * @param inFile the input .tar file//ww w . j a v a 2s . c o m * @param outDir the output directory * @throws IOException * @throws FileNotFoundException * @throws ArchiveException * * @return The list of files with the untared content. */ public static List<File> unTar(final File inFile, final File outDir) throws FileNotFoundException, IOException, ArchiveException { logger.debug("Untaring {} to dir {}", inFile, outDir); assert inFile.getName().endsWith(TAR_EXT); final List<File> untaredFiles = new ArrayList<File>(); final InputStream is = new FileInputStream(inFile); final TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); ArchiveEntry entry; while ((entry = tis.getNextEntry()) != null) { final File outFile = new File(outDir, entry.getName()); if (entry.isDirectory()) { logger.debug("Attempting to write output dir {}", outFile); if (!outFile.exists()) { logger.debug("Attempting to create output dir {}", outFile); if (!outFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s", outFile.getAbsolutePath())); } } } else { logger.debug("Creating output file {}", outFile); streamToFile(tis, outFile); } untaredFiles.add(outFile); } tis.close(); return untaredFiles; }
From source file:consulo.cold.runner.execute.target.artifacts.Generator.java
private static void copyEntry(ArchiveOutputStream archiveOutputStream, ArchiveInputStream ais, ArchiveEntry tempEntry, ArchiveEntryWrapper newEntry) throws IOException { byte[] data = null; if (!tempEntry.isDirectory()) { try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) { IOUtils.copy(ais, byteStream); data = byteStream.toByteArray(); }/* w ww . j a v a 2s . co m*/ // change line breaks try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { if (LineEnds.convert(new ByteArrayInputStream(data), stream, LineEnds.STYLE_UNIX)) { data = stream.toByteArray(); } } catch (BinaryDataException ignored) { // ignore binary data } } if (data != null) { newEntry.setSize(data.length); } archiveOutputStream.putArchiveEntry(newEntry.getArchiveEntry()); if (data != null) { IOUtils.copy(new ByteArrayInputStream(data), archiveOutputStream); } archiveOutputStream.closeArchiveEntry(); }
From source file:consulo.cold.runner.execute.target.artifacts.Generator.java
private static int extractMode(ArchiveEntry entry) { if (entry instanceof TarArchiveEntry) { return ((TarArchiveEntry) entry).getMode(); }//ww w .j a va 2s.c o m return entry.isDirectory() ? TarArchiveEntry.DEFAULT_DIR_MODE : TarArchiveEntry.DEFAULT_FILE_MODE; }
From source file:com.amazonaws.codepipeline.jenkinsplugin.ExtractionTools.java
private static void extractArchive(final File destination, final ArchiveInputStream archiveInputStream) throws IOException { final int BUFFER_SIZE = 8192; ArchiveEntry entry = archiveInputStream.getNextEntry(); while (entry != null) { final File destinationFile = getDestinationFile(destination, entry.getName()); if (entry.isDirectory()) { destinationFile.mkdir();// www . j ava2 s .c o m } else { destinationFile.getParentFile().mkdirs(); try (final OutputStream fileOutputStream = new FileOutputStream(destinationFile)) { final byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead; while ((bytesRead = archiveInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, bytesRead); } } } entry = archiveInputStream.getNextEntry(); } }
From source file:com.continuuity.weave.kafka.client.KafkaTest.java
private static File extractKafka() throws IOException, ArchiveException, CompressorException { File kafkaExtract = TMP_FOLDER.newFolder(); InputStream kakfaResource = KafkaTest.class.getClassLoader().getResourceAsStream("kafka-0.7.2.tgz"); ArchiveInputStream archiveInput = new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, new CompressorStreamFactory() .createCompressorInputStream(CompressorStreamFactory.GZIP, kakfaResource)); try {//from w ww . j av a2 s .co m ArchiveEntry entry = archiveInput.getNextEntry(); while (entry != null) { File file = new File(kafkaExtract, entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { ByteStreams.copy(archiveInput, Files.newOutputStreamSupplier(file)); } entry = archiveInput.getNextEntry(); } } finally { archiveInput.close(); } return kafkaExtract; }
From source file:com.petpet.c3po.gatherer.FileExtractor.java
/** * Unpack data from the stream to specified directory. * /*from ww w . j a v a 2 s. c o m*/ * @param in * stream with tar data * @param outputDir * destination directory * @return true in case of success, otherwise - false */ private static void extract(ArchiveInputStream in, File outputDir) { try { ArchiveEntry entry; while ((entry = in.getNextEntry()) != null) { // replace : for windows OS. final File file = new File(outputDir, entry.getName().replaceAll(":", "_")); if (entry.isDirectory()) { if (!file.exists()) { file.mkdirs(); } } else { file.getParentFile().mkdirs(); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE); try { IOUtils.copy(in, out); out.flush(); } finally { try { out.close(); } catch (IOException e) { LOG.debug("An error occurred while closing the output stream for file '{}'. Error: {}", file, e.getMessage()); } } } } } catch (IOException e) { LOG.debug("An error occurred while handling archive file. Error: {}", e.getMessage()); } finally { if (in != null) { try { in.close(); } catch (IOException e) { LOG.debug("An error occurred while closing the archive stream . Error: {}", e.getMessage()); } } } }