List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream close
public void close() throws IOException
From source file:org.codehaus.plexus.archiver.tar.TarArchiverTest.java
public void testCreateArchiveWithJiustASymlink() throws Exception { TarArchiver archiver = getPosixTarArchiver(); archiver.setDirectoryMode(0500);/* w w w . j a v a 2 s . c om*/ archiver.setFileMode(0400); archiver.setFileMode(0640); archiver.setDestFile(getTestFile("target/output/symlinkarchive.tar")); archiver.addSymlink("link_to_test_destinaton", "../test_destination/"); archiver.createArchive(); TarArchiveInputStream tis; tis = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(archiver.getDestFile()))); TarArchiveEntry te; while ((te = tis.getNextTarEntry()) != null) { if (te.isDirectory()) { assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", 0500, te.getMode() & UnixStat.PERM_MASK); } else if (te.isSymbolicLink()) { assertEquals("../test_destination/", te.getLinkName()); assertEquals("link_to_test_destinaton", te.getName()); assertEquals(0640, te.getMode() & UnixStat.PERM_MASK); } else { assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", 0400, te.getMode() & UnixStat.PERM_MASK); } } tis.close(); }
From source file:org.codehaus.plexus.archiver.tar.TarRoundTripTest.java
/** * test round-tripping long (GNU) entries *//*ww w .j a v a 2s . c o m*/ public void testLongRoundTripping() throws IOException { TarArchiveEntry original = new TarArchiveEntry(LONG_NAME); assertEquals("over 100 chars", true, LONG_NAME.length() > 100); assertEquals("original name", LONG_NAME, original.getName()); ByteArrayOutputStream buff = new ByteArrayOutputStream(); TarArchiveOutputStream tos = new TarArchiveOutputStream(buff); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); tos.putArchiveEntry(original); tos.closeArchiveEntry(); tos.close(); TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(buff.toByteArray())); TarArchiveEntry tripped = tis.getNextTarEntry(); assertEquals("round-tripped name", LONG_NAME, tripped.getName()); assertNull("no more entries", tis.getNextEntry()); tis.close(); }
From source file:org.culturegraph.mf.stream.source.TarReader.java
@Override public void process(final Reader reader) { TarArchiveInputStream tarInputStream = null; try {// w w w . j a v a 2 s .c o m tarInputStream = new TarArchiveInputStream(new ReaderInputStream(reader)); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) { if (!entry.isDirectory()) { final byte[] buffer = new byte[(int) entry.getSize()]; while ((tarInputStream.read(buffer)) > 0) { getReceiver().process(new StringReader(new String(buffer))); } } } tarInputStream.close(); } catch (final FileNotFoundException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(tarInputStream); } }
From source file:org.dataconservancy.ui.util.TarPackageExtractor.java
@Override protected List<File> unpackFilesFromStream(InputStream packageInputStream, String packageDir, String fileName) throws UnpackException { final TarArchiveInputStream tarInStream; if (!TarArchiveInputStream.class.isAssignableFrom(packageInputStream.getClass())) { tarInStream = new TarArchiveInputStream(packageInputStream); } else {//from ww w . j a v a2s . c o m tarInStream = (TarArchiveInputStream) packageInputStream; } List<File> files = new ArrayList<File>(); try { TarArchiveEntry entry = tarInStream.getNextTarEntry(); //Get next tar entry returns null when there are no more entries while (entry != null) { //Directories are automatically handled by the base class so we can ignore them in this class. if (!entry.isDirectory()) { File entryFile = new File(packageDir, entry.getName()); if (entryFile != null) { List<File> savedFiles = saveExtractedFile(entryFile, tarInStream); files.addAll(savedFiles); } } entry = tarInStream.getNextTarEntry(); } tarInStream.close(); } catch (IOException e) { final String msg = "Error processing TarArchiveInputStream: " + e.getMessage(); log.error(msg, e); throw new UnpackException(msg, e); } return files; }
From source file:org.datavec.api.util.ArchiveUtils.java
/** * Extracts files to the specified destination * @param file the file to extract to/*from www . ja va2s . co m*/ * @param dest the destination directory * @throws java.io.IOException */ public static void unzipFileTo(String file, String dest) throws IOException { File target = new File(file); if (!target.exists()) throw new IllegalArgumentException("Archive doesnt exist"); FileInputStream fin = new FileInputStream(target); int BUFFER = 2048; byte data[] = new byte[BUFFER]; if (file.endsWith(".zip") || file.endsWith(".jar")) { //getFromOrigin the zip file content ZipInputStream zis = new ZipInputStream(fin); //getFromOrigin the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(dest + File.separator + fileName); if (ze.isDirectory()) { newFile.mkdirs(); zis.closeEntry(); ze = zis.getNextEntry(); continue; } log.info("file unzip : " + newFile.getAbsoluteFile()); //create all non exists folders //else you will hit FileNotFoundException for compressed folder FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(data)) > 0) { fos.write(data, 0, len); } fos.flush(); fos.close(); zis.closeEntry(); ze = zis.getNextEntry(); } zis.close(); } else if (file.endsWith(".tar")) { BufferedInputStream in = new BufferedInputStream(fin); TarArchiveInputStream tarIn = new TarArchiveInputStream(in); TarArchiveEntry entry = null; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /** If the entry is a directory, createComplex the directory. **/ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /** * If the entry is a file,write the decompressed file to the disk * and close destination stream. **/ else { int count; FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); ; IOUtils.closeQuietly(destStream); } } /** Close the input stream **/ tarIn.close(); } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) { BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); TarArchiveEntry entry = null; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /** If the entry is a directory, createComplex the directory. **/ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /** * If the entry is a file,write the decompressed file to the disk * and close destination stream. **/ else { int count; FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); IOUtils.closeQuietly(destStream); } } /** Close the input stream **/ tarIn.close(); } else if (file.endsWith(".gz")) { GZIPInputStream is2 = new GZIPInputStream(fin); File extracted = new File(target.getParent(), target.getName().replace(".gz", "")); if (extracted.exists()) extracted.delete(); extracted.createNewFile(); OutputStream fos = FileUtils.openOutputStream(extracted); IOUtils.copyLarge(is2, fos); is2.close(); fos.flush(); fos.close(); } }
From source file:org.deeplearning4j.util.ArchiveUtils.java
/** * Extracts files to the specified destination * @param file the file to extract to/* w ww. ja v a 2 s . c om*/ * @param dest the destination directory * @throws IOException */ public static void unzipFileTo(String file, String dest) throws IOException { File target = new File(file); if (!target.exists()) throw new IllegalArgumentException("Archive doesnt exist"); FileInputStream fin = new FileInputStream(target); int BUFFER = 2048; byte data[] = new byte[BUFFER]; if (file.endsWith(".zip")) { //getFromOrigin the zip file content ZipInputStream zis = new ZipInputStream(fin); //getFromOrigin the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(dest + File.separator + fileName); log.info("file unzip : " + newFile.getAbsoluteFile()); //createComplex all non exists folders //else you will hit FileNotFoundException for compressed folder new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(data)) > 0) { fos.write(data, 0, len); } fos.close(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) { BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); TarArchiveEntry entry = null; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /** If the entry is a directory, createComplex the directory. **/ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /** * If the entry is a file,write the decompressed file to the disk * and close destination stream. **/ else { int count; FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); IOUtils.closeQuietly(destStream); } } /** Close the input stream **/ tarIn.close(); } else if (file.endsWith(".gz")) { GZIPInputStream is2 = new GZIPInputStream(fin); File extracted = new File(target.getParent(), target.getName().replace(".gz", "")); if (extracted.exists()) extracted.delete(); extracted.createNewFile(); OutputStream fos = FileUtils.openOutputStream(extracted); IOUtils.copyLarge(is2, fos); is2.close(); fos.flush(); fos.close(); } target.delete(); }
From source file:org.dspace.pack.bagit.Bag.java
public void inflate(InputStream in, String fmt) throws IOException { if (filled) { throw new IllegalStateException("Cannot inflate filled bag"); }// w ww.j av a 2 s. co m if ("zip".equals(fmt)) { ZipInputStream zin = new ZipInputStream(in); ZipEntry entry = null; while ((entry = zin.getNextEntry()) != null) { File outFile = new File(baseDir.getParent(), entry.getName()); outFile.getParentFile().mkdirs(); FileOutputStream fout = new FileOutputStream(outFile); Utils.copy(zin, fout); fout.close(); } zin.close(); } else if ("tgz".equals(fmt)) { TarArchiveInputStream tin = new TarArchiveInputStream(new GzipCompressorInputStream(in)); TarArchiveEntry entry = null; while ((entry = tin.getNextTarEntry()) != null) { File outFile = new File(baseDir.getParent(), entry.getName()); outFile.getParentFile().mkdirs(); FileOutputStream fout = new FileOutputStream(outFile); Utils.copy(tin, fout); fout.close(); } tin.close(); } filled = true; }
From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java
private Map<String, TarArchiveEntry> getTarEntries() throws IOException, FileNotFoundException { TarArchiveInputStream tarStream = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(tarGzArchive))); Map<String, TarArchiveEntry> entries = new HashMap<String, TarArchiveEntry>(); try {//from ww w .j av a2 s .c o m TarArchiveEntry tarEntry = null; while ((tarEntry = tarStream.getNextTarEntry()) != null) { entries.put(tarEntry.getName(), tarEntry); } } finally { tarStream.close(); } return entries; }
From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java
private byte[] getTarEntry(String name) throws IOException { TarArchiveInputStream tarStream = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(tarGzArchive))); try {//w w w . j a va 2 s . c o m TarArchiveEntry tarEntry = null; while ((tarEntry = tarStream.getNextTarEntry()) != null) { if (name.equals(tarEntry.getName())) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(tarStream, baos); return baos.toByteArray(); } } } finally { tarStream.close(); } throw new IOException(name + " not found in " + tarGzArchive); }
From source file:org.exist.xquery.modules.compression.UnTarFunction.java
@Override protected Sequence processCompressedData(BinaryValue compressedData) throws XPathException, XMLDBException { TarArchiveInputStream tis = null; try {/*from w ww .j av a 2 s.c o m*/ tis = new TarArchiveInputStream(compressedData.getInputStream()); TarArchiveEntry entry = null; Sequence results = new ValueSequence(); while ((entry = tis.getNextTarEntry()) != null) { Sequence processCompressedEntryResults = processCompressedEntry(entry.getName(), entry.isDirectory(), tis, filterParam, storeParam); results.addAll(processCompressedEntryResults); } return results; } catch (IOException ioe) { LOG.error(ioe.getMessage(), ioe); throw new XPathException(this, ioe.getMessage(), ioe); } finally { if (tis != null) { try { tis.close(); } catch (IOException ioe) { LOG.warn(ioe.getMessage(), ioe); } } } }