List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream TarArchiveInputStream
public TarArchiveInputStream(InputStream is)
From source file:org.datavec.api.util.ArchiveUtils.java
/** * Extracts files to the specified destination * @param file the file to extract to// w w w .ja va 2 s.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.dcm4chee.storage.tar.TarContainerProvider.java
@Override public InputStream seekEntry(RetrieveContext ctx, String name, String entryName, InputStream in) throws IOException { TarArchiveInputStream tar = new TarArchiveInputStream(in); String checksumEntry = container.getChecksumEntry(); TarArchiveEntry nextEntry;/*from ww w .j a va 2 s.c om*/ while ((nextEntry = tar.getNextTarEntry()) != null) { String nextEntryName = nextEntry.getName(); if (nextEntry.isDirectory() || nextEntryName.equals(checksumEntry)) continue; if (nextEntryName.equals(entryName)) return tar; } throw new ObjectNotFoundException(ctx.getStorageSystem().getStorageSystemPath(), name, entryName); }
From source file:org.dcm4chee.storage.tar.TarContainerProvider.java
@Override public void extractEntries(RetrieveContext ctx, String name, ExtractTask extractTask, InputStream in) throws IOException { TarArchiveInputStream tar = new TarArchiveInputStream(in); TarArchiveEntry entry = skipDirectoryEntries(tar); if (entry == null) throw new IOException("No entries in " + name); String entryName = entry.getName(); Map<String, byte[]> checksums = null; String checksumEntry = container.getChecksumEntry(); MessageDigest digest = null;/*from w w w . j a v a 2 s . c o m*/ if (checksumEntry != null) { if (checksumEntry.equals(entryName)) { try { digest = MessageDigest .getInstance(ctx.getStorageSystem().getStorageSystemGroup().getDigestAlgorithm()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } checksums = ContainerEntry.readChecksumsFrom(tar); } else LOG.warn("Misssing checksum entry in %s", name); entry = skipDirectoryEntries(tar); } for (; entry != null; entry = skipDirectoryEntries(tar)) { entryName = entry.getName(); InputStream in0 = tar; byte[] checksum = null; if (checksums != null && digest != null) { checksum = checksums.remove(entryName); if (checksum == null) throw new ChecksumException( "Missing checksum for container entry: " + entryName + " in " + name); digest.reset(); in0 = new DigestInputStream(tar, digest); } extractTask.copyStream(entryName, in0); if (checksums != null && digest != null) { if (!Arrays.equals(digest.digest(), checksum)) { throw new ChecksumException( "Checksums do not match for container entry: " + entry.getName() + " in " + name); } } extractTask.entryExtracted(entryName); } }
From source file:org.dcm4chee.storage.test.unit.tar.TarContainerProviderTest.java
@Test public void testWriteEntriesTo() throws Exception { Path srcEntryPath = createFile(ENTRY, ENTRY_FILE); Path targetTarPath = dir.getPath().resolve(NAME); try (OutputStream out = Files.newOutputStream(targetTarPath)) { provider.writeEntriesTo(storageCtx, makeEntries(srcEntryPath), out); }/*from w w w . j a v a 2s .co m*/ assertEquals(TAR.length, Files.size(targetTarPath)); try (TarArchiveInputStream expectedTar = new TarArchiveInputStream(new ByteArrayInputStream(TAR)); TarArchiveInputStream actualTar = new TarArchiveInputStream(Files.newInputStream(targetTarPath))) { assertTarEquals(expectedTar, actualTar); } }
From source file:org.deeplearning4j.examples.multigpu.w2vsentiment.DataSetsBuilder.java
private static void extractTarGz(String filePath, String outputPath) throws IOException { int fileCount = 0; int dirCount = 0; System.out.print("Extracting files"); try (TarArchiveInputStream tais = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(filePath))))) { TarArchiveEntry entry;/*from www . j a v a2 s .c om*/ /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tais.getNextEntry()) != null) { //System.out.println("Extracting file: " + entry.getName()); //Create directories as required if (entry.isDirectory()) { new File(outputPath + entry.getName()).mkdirs(); dirCount++; } else { int count; byte data[] = new byte[BUFFER_SIZE]; FileOutputStream fos = new FileOutputStream(outputPath + entry.getName()); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE); while ((count = tais.read(data, 0, BUFFER_SIZE)) != -1) { dest.write(data, 0, count); } dest.close(); fileCount++; } if (fileCount % 1000 == 0) System.out.print("."); } } System.out .println("\n" + fileCount + " files and " + dirCount + " directories extracted to: " + outputPath); }
From source file:org.deeplearning4j.examples.utilities.DataUtilities.java
/** * Extract a "tar.gz" file into a local folder. * @param inputPath Input file path./*from w w w . j a va 2 s. c o m*/ * @param outputPath Output directory path. * @throws IOException IO error. */ public static void extractTarGz(String inputPath, String outputPath) throws IOException { if (inputPath == null || outputPath == null) return; final int bufferSize = 4096; if (!outputPath.endsWith("" + File.separatorChar)) outputPath = outputPath + File.separatorChar; try (TarArchiveInputStream tais = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(inputPath))))) { TarArchiveEntry entry; while ((entry = (TarArchiveEntry) tais.getNextEntry()) != null) { if (entry.isDirectory()) { new File(outputPath + entry.getName()).mkdirs(); } else { int count; byte data[] = new byte[bufferSize]; FileOutputStream fos = new FileOutputStream(outputPath + entry.getName()); BufferedOutputStream dest = new BufferedOutputStream(fos, bufferSize); while ((count = tais.read(data, 0, bufferSize)) != -1) { dest.write(data, 0, count); } dest.close(); } } } }
From source file:org.deeplearning4j.util.ArchiveUtils.java
/** * Extracts files to the specified destination * @param file the file to extract to/*from www .j a v a 2 s .c o m*/ * @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 w w . j a v a 2 s . c om 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.acute.OmnisharpStreamConnectionProvider.java
/** * * @return path to server, unzipping it if necessary. Can be null is fragment is missing. *//* w w w .ja va 2 s . c o m*/ private @Nullable File getServer() throws IOException { File serverPath = new File(AcutePlugin.getDefault().getStateLocation().toFile(), "omnisharp-roslyn"); //$NON-NLS-1$ if (!serverPath.exists()) { serverPath.mkdirs(); try (InputStream stream = FileLocator.openStream(AcutePlugin.getDefault().getBundle(), new Path("omnisharp-roslyn.tar"), true); //$NON-NLS-1$ TarArchiveInputStream tarStream = new TarArchiveInputStream(stream);) { TarArchiveEntry entry = null; while ((entry = tarStream.getNextTarEntry()) != null) { if (!entry.isDirectory()) { File targetFile = new File(serverPath, entry.getName()); targetFile.getParentFile().mkdirs(); InputStream in = new BoundedInputStream(tarStream, entry.getSize()); // mustn't be closed try (FileOutputStream out = new FileOutputStream(targetFile);) { IOUtils.copy(in, out); if (!Platform.OS_WIN32.equals(Platform.getOS())) { int xDigit = entry.getMode() % 10; targetFile.setExecutable(xDigit > 0, (xDigit & 1) == 1); int wDigit = (entry.getMode() / 10) % 10; targetFile.setWritable(wDigit > 0, (wDigit & 1) == 1); int rDigit = (entry.getMode() / 100) % 10; targetFile.setReadable(rDigit > 0, (rDigit & 1) == 1); } } } } } } return serverPath; }
From source file:org.eclipse.cdt.internal.p2.touchpoint.natives.actions.UnpackAction.java
private void untar(InputStream in, File destDir) throws IOException { byte[] buff = new byte[4096]; try (TarArchiveInputStream tarIn = new TarArchiveInputStream(in)) { for (TarArchiveEntry entry = tarIn.getNextTarEntry(); entry != null; entry = tarIn.getNextTarEntry()) { String name = entry.getName(); File destFile = new File(destDir, name); if (entry.isSymbolicLink()) { Files.createSymbolicLink(destFile.toPath(), Paths.get(name)); } else { try (FileOutputStream out = new FileOutputStream(destFile)) { long size = entry.getSize(); while (size > 0) { int n = tarIn.read(buff, 0, (int) Math.min(size, buff.length)); out.write(buff, 0, n); size -= n;//from w w w.j a v a2s . c o m } } chmod(destFile, entry.getMode()); } } } }