List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream getNextTarEntry
public TarArchiveEntry getNextTarEntry() throws IOException
From source file:com.vividsolutions.jump.io.CompressedFile.java
/** * Utility file open function - handles compressed and un-compressed files. * // ww w . java 2s .c om * @param filePath * name of the file to search for. * @param compressedEntry * name of the compressed file. * * <p> * If compressedEntry = null, opens a FileInputStream on filePath * </p> * * <p> * If filePath ends in ".zip" - opens the compressed Zip and * looks for the file called compressedEntry * </p> * * <p> * If filePath ends in ".gz" - opens the compressed .gz file. * </p> */ public static InputStream openFile(String filePath, String compressedEntry) throws IOException { System.out.println(filePath + " extract " + compressedEntry); if (isTar(filePath)) { InputStream is = new BufferedInputStream(new FileInputStream(filePath)); if (filePath.toLowerCase().endsWith("gz")) is = new GzipCompressorInputStream(is, true); else if (filePath.matches("(?i).*bz2?")) is = new BZip2CompressorInputStream(is, true); else if (filePath.matches("(?i).*xz")) is = new XZCompressorInputStream(is, true); TarArchiveInputStream tis = new TarArchiveInputStream(is); if (compressedEntry == null) return is; TarArchiveEntry entry; while ((entry = tis.getNextTarEntry()) != null) { if (entry.getName().equals(compressedEntry)) return tis; } throw createArchiveFNFE(filePath, compressedEntry); } else if (compressedEntry == null && isGZip(filePath)) { // gz compressed file -- easy InputStream is = new BufferedInputStream(new FileInputStream(filePath)); return new GzipCompressorInputStream(is, true); } else if (compressedEntry == null && isBZip(filePath)) { // bz compressed file -- easy InputStream is = new BufferedInputStream(new FileInputStream(filePath)); return new BZip2CompressorInputStream(is, true); //return new org.itadaki.bzip2.BZip2InputStream(is, false); } else if (compressedEntry == null && isXZ(filePath)) { InputStream is = new BufferedInputStream(new FileInputStream(filePath)); return new XZCompressorInputStream(is, true); } else if (compressedEntry != null && isZip(filePath)) { ZipFile zipFile = new ZipFile(filePath); ZipArchiveEntry zipEntry = zipFile.getEntry(compressedEntry); if (zipEntry != null) return zipFile.getInputStream(zipEntry); throw createArchiveFNFE(filePath, compressedEntry); } else if (compressedEntry != null && isSevenZ(filePath)) { SevenZFileGiveStream sevenZFile = new SevenZFileGiveStream(new File(filePath)); SevenZArchiveEntry entry; while ((entry = sevenZFile.getNextEntry()) != null) { if (entry.getName().equals(compressedEntry)) return sevenZFile.getCurrentEntryInputStream(); } throw createArchiveFNFE(filePath, compressedEntry); } // return plain stream if no compressedEntry else if (compressedEntry == null) { return new FileInputStream(filePath); } else { throw new IOException("Couldn't determine compressed file type for file '" + filePath + "' supposedly containing '" + compressedEntry + "'."); } }
From source file:com.sshtools.common.vomanagementtool.common.VOHelper.java
private static void untar(InputStream in, String untarDir) throws IOException { TarArchiveInputStream tin = new TarArchiveInputStream(in); TarArchiveEntry entry = tin.getNextTarEntry(); //TarInputStream tin = new TarInputStream(in); //TarEntry tarEntry = tin.getNextEntry(); if (new File(untarDir).exists()) { //To handle the normal files (not symbolic files) while (entry != null) { if (!entry.isSymbolicLink()) { String filename = untarDir + File.separatorChar + entry.getName(); File destPath = new File(filename); if (!entry.isDirectory()) { FileOutputStream fout = new FileOutputStream(destPath); IOUtils.copy(tin, fout); //tin.copyEntryContents(fout); fout.close();/*ww w. j ava 2 s .c o m*/ } else { if (!destPath.exists()) { boolean success = destPath.mkdir(); if (!success) { throw new IOException("Couldn't create directory for VOMS support: " + destPath.getAbsolutePath()); } } } } entry = tin.getNextTarEntry(); //tarEntry = tin.getNextEntry(); } //tin.close(); //To handle the symbolic link files tin = new TarArchiveInputStream(in); entry = tin.getNextTarEntry(); while (entry != null) { if (entry.isSymbolicLink()) { File srcPath = new File(untarDir + File.separatorChar + entry.getLinkName()); File destPath = new File(untarDir + File.separatorChar + entry.getName()); copyFile(srcPath, destPath); //tarEntry = tin.getNextEntry(); } entry = tin.getNextTarEntry(); } tin.close(); } else { throw new IOException("Couldn't find directory: " + untarDir); } }
From source file:eu.eubrazilcc.lvl.core.io.FileCompressor.java
/** * Uncompress the content of a tarball file to the specified directory. * @param srcFile - the tarball to be uncompressed * @param outDir - directory where the files (and directories) extracted from the tarball will be written * @return the list of files (and directories) extracted from the tarball * @throws IOException when an exception occurs on uncompressing the tarball */// w ww . j a va 2 s.c om public static List<String> unGzipUnTar(final String srcFile, final String outDir) throws IOException { final List<String> uncompressedFiles = newArrayList(); FileInputStream fileInputStream = null; BufferedInputStream bufferedInputStream = null; GzipCompressorInputStream gzipCompressorInputStream = null; TarArchiveInputStream tarArchiveInputStream = null; BufferedInputStream bufferedInputStream2 = null; try { forceMkdir(new File(outDir)); fileInputStream = new FileInputStream(srcFile); bufferedInputStream = new BufferedInputStream(fileInputStream); gzipCompressorInputStream = new GzipCompressorInputStream(fileInputStream); tarArchiveInputStream = new TarArchiveInputStream(gzipCompressorInputStream); TarArchiveEntry entry = null; while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) { final File outputFile = new File(outDir, entry.getName()); if (entry.isDirectory()) { // attempting to write output directory if (!outputFile.exists()) { // attempting to create output directory if (!outputFile.mkdirs()) { throw new IOException("Cannot create directory: " + outputFile.getCanonicalPath()); } } } else { // attempting to create output file final byte[] content = new byte[(int) entry.getSize()]; tarArchiveInputStream.read(content, 0, content.length); final FileOutputStream outputFileStream = new FileOutputStream(outputFile); write(content, outputFileStream); outputFileStream.close(); uncompressedFiles.add(outputFile.getCanonicalPath()); } } } finally { if (bufferedInputStream2 != null) { bufferedInputStream2.close(); } if (tarArchiveInputStream != null) { tarArchiveInputStream.close(); } if (gzipCompressorInputStream != null) { gzipCompressorInputStream.close(); } if (bufferedInputStream != null) { bufferedInputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } return uncompressedFiles; }
From source file:lucee.commons.io.compress.CompressUtil.java
private static void extractTar(Resource tarFile, Resource targetDir) throws IOException { if (!targetDir.exists() || !targetDir.isDirectory()) throw new IOException(targetDir + " is not a existing directory"); if (!tarFile.exists()) throw new IOException(tarFile + " is not a existing file"); if (tarFile.isDirectory()) { Resource[] files = tarFile.listResources(new ExtensionResourceFilter("tar")); if (files == null) throw new IOException("directory " + tarFile + " is empty"); extract(FORMAT_TAR, files, targetDir); return;//from w w w . j av a 2 s . co m } // read the zip file and build a query from its contents TarArchiveInputStream tis = null; try { tis = new TarArchiveInputStream(IOUtil.toBufferedInputStream(tarFile.getInputStream())); TarArchiveEntry entry; int mode; while ((entry = tis.getNextTarEntry()) != null) { //print.ln(entry); Resource target = targetDir.getRealResource(entry.getName()); if (entry.isDirectory()) { target.mkdirs(); } else { Resource parent = target.getParentResource(); if (!parent.exists()) parent.mkdirs(); IOUtil.copy(tis, target, false); } target.setLastModified(entry.getModTime().getTime()); mode = entry.getMode(); if (mode > 0) target.setMode(mode); //tis.closeEntry() ; } } finally { IOUtil.closeEL(tis); } }
From source file:frameworks.Masken.java
public static void uncompressTarGZ(File tarFile, File dest) throws IOException { dest.mkdir();//from w ww . jav a2 s. co m TarArchiveInputStream tarIn = null; tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile)))); TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); // tarIn is a TarArchiveInputStream while (tarEntry != null) {// create a file with the same name as the tarEntry File destPath = new File(dest, tarEntry.getName()); System.out.println("working: " + destPath.getCanonicalPath()); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { if (!destPath.getParentFile().exists()) { destPath.getParentFile().mkdirs(); } destPath.createNewFile(); //byte [] btoRead = new byte[(int)tarEntry.getSize()]; byte[] btoRead = new byte[1024]; //FileInputStream fin // = new FileInputStream(destPath.getCanonicalPath()); BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); int len = 0; while ((len = tarIn.read(btoRead)) != -1) { bout.write(btoRead, 0, len); } bout.close(); btoRead = null; } tarEntry = tarIn.getNextTarEntry(); } tarIn.close(); }
From source file:adams.core.io.TarUtils.java
/** * Lists the files stored in the tar file. * * @param input the tar file to obtain the file list from * @param listDirs whether to include directories in the list * @return the stored files//from w ww .ja v a2 s .c o m */ public static List<File> listFiles(File input, boolean listDirs) { List<File> result; FileInputStream fis; TarArchiveInputStream archive; TarArchiveEntry entry; result = new ArrayList<>(); archive = null; fis = null; try { fis = new FileInputStream(input.getAbsolutePath()); archive = openArchiveForReading(input, fis); while ((entry = archive.getNextTarEntry()) != null) { if (entry.isDirectory()) { if (listDirs) result.add(new File(entry.getName())); } else { result.add(new File(entry.getName())); } } } catch (Exception e) { e.printStackTrace(); } finally { FileUtils.closeQuietly(fis); if (archive != null) { try { archive.close(); } catch (Exception e) { // ignored } } } return result; }
From source file:com.anthemengineering.mojo.infer.InferMojo.java
/** * Extracts a given infer.tar.xz file to the given directory. * * @param tarXzToExtract the file to extract * @param inferDownloadDir the directory to extract the file to */// w w w . ja va 2 s. co m private static void extract(File tarXzToExtract, File inferDownloadDir) throws IOException { FileInputStream fin = null; BufferedInputStream in = null; XZCompressorInputStream xzIn = null; TarArchiveInputStream tarIn = null; try { fin = new FileInputStream(tarXzToExtract); in = new BufferedInputStream(fin); xzIn = new XZCompressorInputStream(in); tarIn = new TarArchiveInputStream(xzIn); TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { final File fileToWrite = new File(inferDownloadDir, entry.getName()); if (entry.isDirectory()) { FileUtils.forceMkdir(fileToWrite); } else { BufferedOutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(fileToWrite)); final byte[] buffer = new byte[4096]; int n = 0; while (-1 != (n = tarIn.read(buffer))) { out.write(buffer, 0, n); } } finally { if (out != null) { out.close(); } } } // assign file permissions final int mode = entry.getMode(); fileToWrite.setReadable((mode & 0004) != 0, false); fileToWrite.setReadable((mode & 0400) != 0, true); fileToWrite.setWritable((mode & 0002) != 0, false); fileToWrite.setWritable((mode & 0200) != 0, true); fileToWrite.setExecutable((mode & 0001) != 0, false); fileToWrite.setExecutable((mode & 0100) != 0, true); } } finally { if (tarIn != null) { tarIn.close(); } } }
From source file:com.cloudbees.clickstack.util.Files2.java
/** * TODO recopy file permissions/*from ww w . jav a2s . com*/ * <p/> * Uncompress the specified tgz file to the specified destination directory. * Replaces any files in the destination, if they already exist. * * @param tgzFile the name of the zip file to extract * @param destDir the directory to unzip to * @throws RuntimeIOException */ public static void untgz(@Nonnull Path tgzFile, @Nonnull Path destDir) throws RuntimeIOException { try { //if the destination doesn't exist, create it if (Files.notExists(destDir)) { logger.trace("Create dir: {}", destDir); Files.createDirectories(destDir); } TarArchiveInputStream in = new TarArchiveInputStream( new GzipCompressorInputStream(Files.newInputStream(tgzFile))); TarArchiveEntry entry; while ((entry = in.getNextTarEntry()) != null) { if (entry.isDirectory()) { Path dir = destDir.resolve(entry.getName()); logger.trace("Create dir {}", dir); Files.createDirectories(dir); } else { Path file = destDir.resolve(entry.getName()); logger.trace("Create file {}: {} bytes", file, entry.getSize()); OutputStream out = Files.newOutputStream(file); IOUtils.copy(in, out); out.close(); } } in.close(); } catch (IOException e) { throw new RuntimeIOException("Exception expanding " + tgzFile + " to " + destDir, e); } }
From source file:indexing.WTDocIndexer.java
void indexTarArchive() throws Exception { GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(new File(tarArchivePath))); TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream); TarArchiveEntry tarEntry = tarArchiveInputStream.getNextTarEntry(); while (tarEntry != null) { if (!tarEntry.isDirectory()) { System.out.println("Extracting " + tarEntry.getName()); final OutputStream outputFileStream = new FileOutputStream(tarEntry.getName()); } else {/* w w w .java2s .c o m*/ tarEntry = tarArchiveInputStream.getNextTarEntry(); } } }
From source file:deployer.publishers.TarFileChecker.java
public void check(TarArchiveInputStream inputStream) throws Exception { // copy the existing entries TarArchiveEntry nextEntry;/*from w ww. j a v a 2 s . c o m*/ while ((nextEntry = inputStream.getNextTarEntry()) != null) { verify(nextEntry, inputStream); } }