List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream getNextEntry
public ArchiveEntry getNextEntry() throws IOException
From source file:jetbrains.exodus.util.CompressBackupUtilTest.java
@Test public void testFolderArchived() throws Exception { File src = new File(randName); src.mkdir();/*from w w w .j a va2s . com*/ FileWriter fw = new FileWriter(new File(src, "1.txt")); fw.write("12345"); fw.close(); fw = new FileWriter(new File(src, "2.txt")); fw.write("12"); fw.close(); CompressBackupUtil.tar(src, dest); Assert.assertTrue("No destination archive created", dest.exists()); TarArchiveInputStream tai = new TarArchiveInputStream( new GZIPInputStream(new BufferedInputStream(new FileInputStream(dest)))); ArchiveEntry entry1 = tai.getNextEntry(); ArchiveEntry entry2 = tai.getNextEntry(); if (entry1.getName().compareTo(entry2.getName()) > 0) { // kinda sort them lol ArchiveEntry tmp = entry1; entry1 = entry2; entry2 = tmp; } Assert.assertNotNull("No entry found in destination archive", entry1); Assert.assertEquals("Entry has wrong size", 5, entry1.getSize()); System.out.println(entry1.getName()); Assert.assertEquals("Entry has wrong relative path", src.getName() + "/1.txt", entry1.getName()); System.out.println(entry2.getName()); Assert.assertEquals("Entry has wrong size", 2, entry2.getSize()); Assert.assertEquals("Entry has wrong relative path", src.getName() + "/2.txt", entry2.getName()); }
From source file:net.ovres.tuxcourser.TarStreamCourseInput.java
/** * Create a stream corresponding to the given file. * This stream should be closed when it is not needed * any more.// w w w .ja v a2s.co m * * This file should be in the main directory, but this * is not validated. * * @returns stream corresponding to the given file. */ public InputStream getInputStream(String filename) throws FileNotFoundException, IOException { String altFilename = filename.replaceAll(".rgb$", ".png"); // First, get a stream we can use... InputStream in = new FileInputStream(source); // Next, decompress it if possible... in = new UncompressedInputStream(in); // Now the tar file... TarArchiveInputStream tar = new TarArchiveInputStream(in); ArchiveEntry entry = null; ArchiveEntry e; e = tar.getNextEntry(); while (entry == null && e != null) { if ((e.getName().endsWith(filename) || e.getName().endsWith(altFilename)) && e.getName().indexOf(".xvpics") == -1) { entry = e; } else { e = tar.getNextEntry(); } } if (entry == null) { tar.close(); throw new FileNotFoundException("File not found in " + source + ": " + filename); } logger.fine("Using: " + entry.getName()); // We could just return tar at this point... // But it's a bit nicer to clean up the tar file // immediately, and return a byte array stream instead. int size = (int) (entry.getSize()); byte[] b = new byte[size]; int num = tar.read(b, 0, size); if (num < size) { logger.warning("Tried to read " + size + " bytes, got " + num); } ByteArrayInputStream intemp; intemp = new ByteArrayInputStream(b); // finally, clean up the various input streams tar.close(); in.close(); return intemp; }
From source file:io.covert.binary.analysis.BuildTarBzSequenceFile.java
@Override public int run(String[] args) throws Exception { File inDir = new File(args[0]); Path name = new Path(args[1]); Text key = new Text(); BytesWritable val = new BytesWritable(); Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); if (!fs.exists(name)) { fs.mkdirs(name);/* ww w. java2s . c o m*/ } for (File file : inDir.listFiles()) { Path sequenceName = new Path(name, file.getName() + ".seq"); System.out.println("Writing to " + sequenceName); SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, sequenceName, Text.class, BytesWritable.class, CompressionType.RECORD); if (!file.isFile()) { System.out.println("Skipping " + file + " (not a file) ..."); continue; } final InputStream is = new FileInputStream(file); final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { if (!entry.isDirectory()) { final ByteArrayOutputStream outputFileStream = new ByteArrayOutputStream(); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); byte[] outputFile = outputFileStream.toByteArray(); val.set(outputFile, 0, outputFile.length); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(outputFile); byte[] digest = md.digest(); String hexdigest = ""; for (int i = 0; i < digest.length; i++) { hexdigest += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1); } key.set(hexdigest); writer.append(key, val); } } debInputStream.close(); writer.close(); } return 0; }
From source file:io.covert.binary.analysis.BuildSequenceFileFromTarball.java
public void load(FileSystem fs, Configuration conf, File inputTarball, Path outputDir) throws Exception { Text key = new Text(); BytesWritable val = new BytesWritable(); Path sequenceName = new Path(outputDir, inputTarball.getName() + ".seq"); System.out.println("Writing to " + sequenceName); SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, sequenceName, Text.class, BytesWritable.class, CompressionType.RECORD); InputStream is = new FileInputStream(inputTarball); if (inputTarball.toString().toLowerCase().endsWith(".gz")) { is = new GZIPInputStream(is); } else if (inputTarball.toString().toLowerCase().endsWith(".bz") || inputTarball.toString().endsWith(".bz2")) { is.read(); // read 'B' is.read(); // read 'Z' is = new CBZip2InputStream(is); }/*from w w w . jav a 2s.c om*/ final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { if (!entry.isDirectory()) { try { final ByteArrayOutputStream outputFileStream = new ByteArrayOutputStream(); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); byte[] outputFile = outputFileStream.toByteArray(); val.set(outputFile, 0, outputFile.length); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(outputFile); byte[] digest = md.digest(); String hexdigest = ""; for (int i = 0; i < digest.length; i++) { hexdigest += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1); } key.set(hexdigest); writer.append(key, val); } catch (IOException e) { System.err.println("Warning: tarball may be truncated: " + inputTarball); // Truncated Tarball break; } } } debInputStream.close(); writer.close(); }
From source file:com.baidu.rigel.biplatform.tesseract.util.FileUtils.java
/** * Uncompress the incoming file./*from w w w. j av a 2 s .c o m*/ * * @param inFileName * Name of the file to be uncompressed * @param outFileName * Name of target */ public static String doUncompressFile(String inFileName, String outFileName) throws IOException { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_BEGIN, "doUncompressFile", "[inFileName:" + inFileName + "]")); if (StringUtils.isEmpty(inFileName)) { throw new IllegalArgumentException(); } String decompressedFileName = outFileName; File inFile = new File(inFileName); if (StringUtils.isEmpty(decompressedFileName)) { // not specified outFileName StringBuilder sb = new StringBuilder(); sb.append(inFile.getParentFile().getAbsolutePath()); sb.append(File.separator); // sb.append(inFile.getName().substring(0, // inFile.getName().indexOf("."))); // sb.append(File.separator); decompressedFileName = sb.toString(); } File outFile = new File(decompressedFileName); if (!outFile.exists()) { outFile.mkdirs(); } /** * create a TarArchiveInputStream object. */ FileInputStream fin = null; BufferedInputStream in = null; GzipCompressorInputStream gzIn = null; TarArchiveInputStream tarIn = null; FileOutputStream fos = null; BufferedOutputStream dest = null; TarArchiveEntry entry = null; try { fin = new FileInputStream(inFile); in = new BufferedInputStream(fin); gzIn = new GzipCompressorInputStream(in); tarIn = new TarArchiveInputStream(gzIn); /** * Read the tar entries using the getNextEntry method */ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_DECOMPRESS_PROCESS, "Extracting File:" + entry.getName())); if (entry.isDirectory()) { /** * If the entry is a directory, create the directory. */ File f = new File(decompressedFileName + entry.getName()); f.mkdirs(); } else { /** * If the entry is a file,write the decompressed file to the * disk and close destination stream. */ int count; byte[] data = new byte[TesseractConstant.FILE_BLOCK_SIZE]; String fileName = decompressedFileName + entry.getName().substring( entry.getName().indexOf(TesseractConstant.DECOMPRESSION_FILENAME_SPLITTER) + 1); fos = new FileOutputStream(new File(fileName)); dest = new BufferedOutputStream(fos, TesseractConstant.FILE_BLOCK_SIZE); while ((count = tarIn.read(data, 0, TesseractConstant.FILE_BLOCK_SIZE)) != -1) { dest.write(data, 0, count); } dest.close(); } } /** * Close the input stream */ tarIn.close(); } catch (IOException e) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_DECOMPRESS_ERROR, "IOException")); LOGGER.error(e.getMessage(), e); throw e; } finally { try { fin.close(); in.close(); gzIn.close(); tarIn.close(); fos.close(); dest.close(); } catch (IOException e) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_DECOMPRESS_ERROR, "IOException occur when closing fd")); LOGGER.error(e.getMessage(), e); throw e; } } LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_END, "doUncompressFile", "[inFileName:" + inFileName + "]")); return decompressedFileName; }
From source file:com.nike.cerberus.operation.dashboard.PublishDashboardOperation.java
private File extractArtifact(final URL artifactUrl) { final File extractionDirectory = Files.createTempDir(); logger.debug("Extracting artifact contents to {}", extractionDirectory.getAbsolutePath()); ArchiveEntry entry;/*w ww . ja v a2s .co m*/ TarArchiveInputStream tarArchiveInputStream = null; try { tarArchiveInputStream = new TarArchiveInputStream( new GzipCompressorInputStream(artifactUrl.openStream())); entry = tarArchiveInputStream.getNextEntry(); while (entry != null) { String entryName = entry.getName(); if (entry.getName().startsWith("./")) { entryName = entry.getName().substring(1); } final File destPath = new File(extractionDirectory, entryName); if (!entry.isDirectory()) { final File fileParentDir = new File(destPath.getParent()); if (!fileParentDir.exists()) { FileUtils.forceMkdir(fileParentDir); } FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(destPath); IOUtils.copy(tarArchiveInputStream, fileOutputStream); } finally { IOUtils.closeQuietly(fileOutputStream); } } entry = tarArchiveInputStream.getNextEntry(); } } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(tarArchiveInputStream); } return extractionDirectory; }
From source file:com.github.nethad.clustermeister.integration.JPPFTestNode.java
private void untar(File tarFile) throws IOException { logger.info("Untar {}.", tarFile.getAbsolutePath()); TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new FileInputStream(tarFile)); try {/*from w w w.j av a2s. co m*/ ArchiveEntry tarEntry = tarArchiveInputStream.getNextEntry(); while (tarEntry != null) { File destPath = new File(libDir, tarEntry.getName()); logger.info("Unpacking {}.", destPath.getAbsoluteFile()); if (!tarEntry.isDirectory()) { FileOutputStream fout = new FileOutputStream(destPath); final byte[] buffer = new byte[8192]; int n = 0; while (-1 != (n = tarArchiveInputStream.read(buffer))) { fout.write(buffer, 0, n); } fout.close(); } else { destPath.mkdir(); } tarEntry = tarArchiveInputStream.getNextEntry(); } } finally { tarArchiveInputStream.close(); } }
From source file:com.boxupp.utilities.PuppetUtilities.java
private void extrectFile(String saveFilePath, String moduleDirPath, String moduleName) { try {//from w ww . j a v a 2s . co m File file = new File(saveFilePath); FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); TarArchiveEntry entry = null; int length = 0; File unzipFile = null; while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { if (entry.isDirectory()) { if (length == 0) { File f = new File(moduleDirPath + entry.getName()); f.mkdirs(); unzipFile = f; length++; } else { File f = new File(moduleDirPath + entry.getName()); f.mkdirs(); } } else { int count; byte data[] = new byte[4096]; FileOutputStream fos; fos = new FileOutputStream(moduleDirPath + entry.getName()); BufferedOutputStream dest = new BufferedOutputStream(fos, 4096); while ((count = tarIn.read(data, 0, 4096)) != -1) { dest.write(data, 0, count); } dest.close(); } } File renamedFile = new File(moduleDirPath + "/" + moduleName); if (unzipFile.isDirectory()) { unzipFile.renameTo(renamedFile); } tarIn.close(); } catch (IOException e) { logger.error("Error in unzip the module file :" + e.getMessage()); } }
From source file:air.AirRuntimeGenerator.java
protected void processFlashRuntime(File sdkSourceDirectory, File sdkTargetDirectory) throws Exception { final File runtimeDirectory = new File(sdkSourceDirectory, "runtimes"); final File flashPlayerDirectory = new File(runtimeDirectory, "player"); File[] versions = flashPlayerDirectory.listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.isDirectory() && !"win".equalsIgnoreCase(pathname.getName()) && !"lnx".equalsIgnoreCase(pathname.getName()) && !"mac".equalsIgnoreCase(pathname.getName()); }//w ww .j a v a 2 s . c o m }); // The flash-player 9 is installed directly in the player directory. if (new File(flashPlayerDirectory, "win").exists()) { final File[] extendedVersions = new File[versions.length + 1]; System.arraycopy(versions, 0, extendedVersions, 0, versions.length); extendedVersions[versions.length] = flashPlayerDirectory; versions = extendedVersions; } if (versions != null) { for (final File versionDir : versions) { // If the versionDir is called "player", then this is the home of the flash-player version 9. final String playerVersionString = "player".equalsIgnoreCase(versionDir.getName()) ? "9.0" : versionDir.getName(); final double playerVersion = Double.valueOf(playerVersionString); final NumberFormat doubleFormat = NumberFormat.getInstance(Locale.US); doubleFormat.setMinimumFractionDigits(1); doubleFormat.setMaximumFractionDigits(1); final String version = doubleFormat.format(playerVersion); final File targetDir = new File(sdkTargetDirectory, "com/adobe/flash/runtime/" + version); // Deploy Windows binaries. final File windowsDirectory = new File(versionDir, "win"); if (windowsDirectory.exists()) { // Find out if a flash-player binary exists. File flashPlayerBinary = null; if (new File(windowsDirectory, "FlashPlayerDebugger.exe").exists()) { flashPlayerBinary = new File(windowsDirectory, "FlashPlayerDebugger.exe"); } else if (new File(windowsDirectory, "FlashPlayer.exe").exists()) { flashPlayerBinary = new File(windowsDirectory, "FlashPlayer.exe"); } // If a binary exists, copy it to the target and create a pom for it. if (flashPlayerBinary != null) { if (!targetDir.exists()) { if (!targetDir.mkdirs()) { throw new RuntimeException( "Could not create directory: " + targetDir.getAbsolutePath()); } } final File targetFile = new File(targetDir, "/runtime-" + version + "-win.exe"); copyFile(flashPlayerBinary, targetFile); } } // Deploy Mac binaries. final File macDirectory = new File(versionDir, "mac"); if (macDirectory.exists()) { // Find out if a flash-player binary exists. File flashPlayerBinary = null; if (new File(macDirectory, "Flash Player.app.zip").exists()) { flashPlayerBinary = new File(macDirectory, "Flash Player.app.zip"); } else if (new File(macDirectory, "Flash Player Debugger.app.zip").exists()) { flashPlayerBinary = new File(macDirectory, "Flash Player Debugger.app.zip"); } // If a binary exists, copy it to the target and create a pom for it. if (flashPlayerBinary != null) { if (!targetDir.exists()) { if (!targetDir.mkdirs()) { throw new RuntimeException( "Could not create directory: " + targetDir.getAbsolutePath()); } } final File targetFile = new File(targetDir, "/runtime-" + version + "-mac.zip"); copyFile(flashPlayerBinary, targetFile); } } // Deploy Linux binaries. final File lnxDirectory = new File(versionDir, "lnx"); if (lnxDirectory.exists()) { // Find out if a flash-player binary exists. File flashPlayerBinary = null; if (new File(lnxDirectory, "flashplayer.tar.gz").exists()) { flashPlayerBinary = new File(lnxDirectory, "flashplayer.tar.gz"); } else if (new File(lnxDirectory, "flashplayerdebugger.tar.gz").exists()) { flashPlayerBinary = new File(lnxDirectory, "flashplayerdebugger.tar.gz"); } // Decompress the archive. // First unzip it. final FileInputStream fin = new FileInputStream(flashPlayerBinary); final BufferedInputStream in = new BufferedInputStream(fin); final File tempTarFile = File.createTempFile("flex-sdk-linux-flashplayer-binary-" + version, ".tar"); final FileOutputStream out = new FileOutputStream(tempTarFile); final GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); final byte[] buffer = new byte[1024]; int n; while (-1 != (n = gzIn.read(buffer))) { out.write(buffer, 0, n); } out.close(); gzIn.close(); // Then untar it. File uncompressedBinary = null; final FileInputStream tarFileInputStream = new FileInputStream(tempTarFile); final TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream( tarFileInputStream); ArchiveEntry entry; while ((entry = tarArchiveInputStream.getNextEntry()) != null) { if ("flashplayer".equals(entry.getName())) { uncompressedBinary = File.createTempFile("flex-sdk-linux-flashplayer-binary-" + version, ".uexe"); final FileOutputStream uncompressedBinaryOutputStream = new FileOutputStream( uncompressedBinary); while (-1 != (n = tarArchiveInputStream.read(buffer))) { uncompressedBinaryOutputStream.write(buffer, 0, n); } uncompressedBinaryOutputStream.close(); } else if ("flashplayerdebugger".equals(entry.getName())) { uncompressedBinary = File.createTempFile("flex-sdk-linux-flashplayer-binary-" + version, ".uexe"); final FileOutputStream uncompressedBinaryOutputStream = new FileOutputStream( uncompressedBinary); while (-1 != (n = tarArchiveInputStream.read(buffer))) { uncompressedBinaryOutputStream.write(buffer, 0, n); } uncompressedBinaryOutputStream.close(); } } tarFileInputStream.close(); // If a binary exists, copy it to the target and create a pom for it. if (uncompressedBinary != null) { if (!targetDir.exists()) { if (!targetDir.mkdirs()) { throw new RuntimeException( "Could not create directory: " + targetDir.getAbsolutePath()); } } final File targetFile = new File(targetDir, "/runtime-" + version + "-linux.uexe"); copyFile(uncompressedBinary, targetFile); // Clean up in the temp directory. if (!uncompressedBinary.delete()) { System.out.println("Could not delete: " + uncompressedBinary.getAbsolutePath()); } } // Clean up in the temp directory. if (!tempTarFile.delete()) { System.out.println("Could not delete: " + tempTarFile.getAbsolutePath()); } } final MavenMetadata playerArtifact = new MavenMetadata(); playerArtifact.setGroupId("com.adobe.flash"); playerArtifact.setArtifactId("runtime"); playerArtifact.setVersion(version); playerArtifact.setPackaging("exe"); writeDocument(createPomDocument(playerArtifact), new File(targetDir, "runtime-" + version + ".pom")); } } }
From source file:com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.RegistryBackedArchiveProfileBuilder.java
public List<Profile> build(DeploymentConfiguration deploymentConfiguration, String baseOutputPath, SpinnakerArtifact artifact, String archiveName) { String version = artifactService.getArtifactVersion(deploymentConfiguration.getName(), artifact); String archiveObjectName = ProfileRegistry.profilePath(artifact.getName(), version, archiveName + ".tar.gz"); InputStream is;/* w w w . java2s . c om*/ try { is = profileRegistry.getObjectContents(archiveObjectName); } catch (IOException e) { throw new HalException(Problem.Severity.FATAL, "Error retrieving contents of archive " + archiveObjectName, e); } TarArchiveInputStream tis; try { tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is); } catch (ArchiveException e) { throw new HalException(Problem.Severity.FATAL, "Failed to unpack tar archive", e); } try { List<Profile> result = new ArrayList<>(); ArchiveEntry profileEntry = tis.getNextEntry(); while (profileEntry != null) { if (profileEntry.isDirectory()) { profileEntry = tis.getNextEntry(); continue; } String entryName = profileEntry.getName(); String profileName = String.join("/", artifact.getName(), archiveName, entryName); String outputPath = Paths.get(baseOutputPath, archiveName, entryName).toString(); String contents = IOUtils.toString(tis); result.add((new ProfileFactory() { @Override protected void setProfile(Profile profile, DeploymentConfiguration deploymentConfiguration, SpinnakerRuntimeSettings endpoints) { profile.setContents(profile.getBaseContents()); } @Override protected Profile getBaseProfile(String name, String version, String outputFile) { return new Profile(name, version, outputFile, contents); } @Override protected boolean showEditWarning() { return false; } @Override protected ArtifactService getArtifactService() { return artifactService; } @Override public SpinnakerArtifact getArtifact() { return artifact; } @Override protected String commentPrefix() { return null; } }).getProfile(profileName, outputPath, deploymentConfiguration, null)); profileEntry = tis.getNextEntry(); } return result; } catch (IOException e) { throw new HalException(Problem.Severity.FATAL, "Failed to read profile entry", e); } }