List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream TarArchiveOutputStream
public TarArchiveOutputStream(OutputStream os)
From source file:org.jclouds.docker.compute.features.internal.Archives.java
public static File tar(File baseDir, File tarFile) throws IOException { // Check that the directory is a directory, and get its contents checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir); File[] files = baseDir.listFiles(); String token = getLast(Splitter.on("/").split(baseDir.getAbsolutePath())); byte[] buf = new byte[1024]; int len;//from w w w . j a v a 2 s . co m TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString()))); tos.putArchiveEntry(tarEntry); if (!file.isDirectory()) { FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); while ((len = in.read(buf)) != -1) { tos.write(buf, 0, len); } in.close(); } tos.closeArchiveEntry(); } tos.close(); return tarFile; }
From source file:org.jclouds.docker.features.internal.Archives.java
public static File tar(File baseDir, File tarFile) throws IOException { // Check that the directory is a directory, and get its contents checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir); File[] files = baseDir.listFiles(); String token = getLast(Splitter.on("/").split(baseDir.getAbsolutePath())); TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); try {//from w ww . j a v a 2 s .c o m for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString()))); tos.putArchiveEntry(tarEntry); if (!file.isDirectory()) { Files.asByteSource(file).copyTo(tos); } tos.closeArchiveEntry(); } } finally { tos.close(); } return tarFile; }
From source file:org.jenkinsci.plugins.os_ci.utils.CompressUtils.java
public static void tarGzDirectory(String baseDir, String targetFile) throws FileNotFoundException, IOException { FileOutputStream fOut = null; BufferedOutputStream bOut = null; TarArchiveOutputStream tOut = null;/* w w w .j a v a2 s . c om*/ GzipCompressorOutputStream gzOut = null; try { System.out.println(new File(".").getAbsolutePath()); fOut = new FileOutputStream(new File(targetFile)); bOut = new BufferedOutputStream(fOut); gzOut = new GzipCompressorOutputStream(bOut); tOut = new TarArchiveOutputStream(gzOut); tOut.setLongFileMode(LONGFILE_POSIX); addFileToTarGz(tOut, baseDir, ""); } finally { if (tOut != null) { tOut.finish(); tOut.close(); } if (gzOut != null) { gzOut.close(); } if (bOut != null) { bOut.close(); } if (fOut != null) { fOut.close(); } } }
From source file:org.kitesdk.cli.commands.TestTarImportCommand.java
@BeforeClass public static void createTestInputFiles() throws IOException { TestTarImportCommand.cleanup();// w w w . ja va2 s .c o m Path testData = new Path(TEST_DATA_DIR); FileSystem testFS = testData.getFileSystem(new Configuration()); datasetUri = "dataset:file:" + System.getProperty("user.dir") + "/" + TEST_DATASET_DIR + "/" + TEST_DATASET_NAME; TarArchiveOutputStream tosNoCompression = null; TarArchiveOutputStream tosGzipCompression = null; TarArchiveOutputStream tosBzip2Compression = null; TarArchiveOutputStream tosLargeEntry = null; TarArchiveEntry tarArchiveEntry = null; try { // No compression tosNoCompression = new TarArchiveOutputStream(testFS.create(new Path(TAR_TEST_FILE), true)); writeToTarFile(tosNoCompression, TAR_TEST_ROOT_PREFIX + "/", null); // Gzip compression tosGzipCompression = new TarArchiveOutputStream( new GzipCompressorOutputStream(testFS.create(new Path(TAR_TEST_GZIP_FILE), true))); writeToTarFile(tosGzipCompression, TAR_TEST_GZIP_ROOT_PREFIX + "/", null); // BZip2 compression tosBzip2Compression = new TarArchiveOutputStream( new BZip2CompressorOutputStream(testFS.create(new Path(TAR_TEST_BZIP2_FILE), true))); writeToTarFile(tosBzip2Compression, TAR_TEST_BZIP2_ROOT_PREFIX + "/", null); // "Large" entry file (10000 bytes) tosLargeEntry = new TarArchiveOutputStream(testFS.create(new Path(TAR_TEST_LARGE_ENTRY_FILE), true)); String largeEntry = RandomStringUtils.randomAscii(10000); writeToTarFile(tosLargeEntry, "largeEntry", largeEntry); // Generate test files with random names and content Random random = new Random(1); for (int i = 0; i < NUM_TEST_FILES; ++i) { // Create random file and data int fNameLength = random.nextInt(MAX_FILENAME_LENGTH); int fContentLength = random.nextInt(MAX_FILECONTENT_LENGTH); String fName = RandomStringUtils.randomAlphanumeric(fNameLength); String fContent = RandomStringUtils.randomAscii(fContentLength); // Write the file to tarball writeToTarFile(tosNoCompression, TAR_TEST_ROOT_PREFIX + "/" + fName, fContent); writeToTarFile(tosGzipCompression, TAR_TEST_GZIP_ROOT_PREFIX + "/" + fName, fContent); writeToTarFile(tosBzip2Compression, TAR_TEST_BZIP2_ROOT_PREFIX + "/" + fName, fContent); System.out.println("Wrote " + fName + " [" + fContentLength + "]"); } } finally { IOUtils.closeStream(tosNoCompression); IOUtils.closeStream(tosGzipCompression); IOUtils.closeStream(tosBzip2Compression); IOUtils.closeStream(tosLargeEntry); } }
From source file:org.mitre.mpf.wfm.service.component.TestStartupComponentRegistrationService.java
private List<Path> addComponentPackages(String... names) throws IOException { List<Path> results = new ArrayList<>(); for (String name : names) { Path packagePath = _componentUploadDir.newFile(name + ".tar.gz").toPath(); try (TarArchiveOutputStream outputStream = new TarArchiveOutputStream( new GZIPOutputStream(Files.newOutputStream(packagePath)))) { ArchiveEntry entry = new TarArchiveEntry(name + "/descriptor"); outputStream.putArchiveEntry(entry); outputStream.closeArchiveEntry(); }/*ww w .ja v a2s . c o m*/ results.add(packagePath); } return results; }
From source file:org.opentestsystem.delivery.testreg.transformer.TarBundler.java
/** * Bundles the inputs into a tar which is returned as a byte stream. The input is an array of byte arrays in which * the first element is a filename and the second element is the actual file. Subsequent files follow this same * pattern. If there is an uneven number of elements then the last file will be ignored. * /*from w ww . j ava 2 s . c o m*/ * @param inputs * @return */ @Transformer public Message<File> bundleToTar(final String[] inputs, final @Header("dwBatchUuid") String dwBatchUuid, final @Header("fileSuffix") String fileSuffix, final @Header("recordsSent") int recordsSent, final @Header("tempPaths") List<Path> tempPaths, final @Header("dwConfigType") DwConfigType dwConfigType) { String debugPrefix = dwConfigType + " DW Config: "; long curTime = System.currentTimeMillis(); File tmpTarFile; FileOutputStream tmpTarOutStream; FileInputStream tmpCsvInStream; FileInputStream tmpJsonInStream; try { Path tmpTarPath = Files.createTempFile(DwBatchHandler.DW_TAR_TMP_PREFIX, (dwConfigType == DwConfigType.SBAC ? DwBatchHandler.SBAC_DW_NAME : DwBatchHandler.LOCAL_DW_NAME) + fileSuffix); tempPaths.add(tmpTarPath); tmpTarFile = tmpTarPath.toFile(); LOGGER.debug(debugPrefix + "Created temp TAR file " + tmpTarFile.getAbsolutePath()); tmpTarOutStream = new FileOutputStream(tmpTarFile); tmpCsvInStream = new FileInputStream(inputs[1]); tmpJsonInStream = new FileInputStream(inputs[4]); TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(tmpTarOutStream); String csvFilename = inputs[0]; String jsonFilename = inputs[3]; // tar archive entry for the csv file TarArchiveEntry entry = new TarArchiveEntry(csvFilename); entry.setSize(Integer.valueOf(inputs[2])); tarOutput.putArchiveEntry(entry); byte[] buf = new byte[BUFFER_SIZE]; int len; while ((len = tmpCsvInStream.read(buf)) > 0) { tarOutput.write(buf, 0, len); } tarOutput.closeArchiveEntry(); // tar archive entry for the json file entry = new TarArchiveEntry(jsonFilename); entry.setSize(Integer.valueOf(inputs[5])); tarOutput.putArchiveEntry(entry); buf = new byte[BUFFER_SIZE]; while ((len = tmpJsonInStream.read(buf)) > 0) { tarOutput.write(buf, 0, len); } tarOutput.closeArchiveEntry(); tarOutput.close(); tmpCsvInStream.close(); tmpJsonInStream.close(); } catch (IOException e) { throw new TarBundlerException(debugPrefix + "failure to tar output streams", e); } LOGGER.debug(debugPrefix + "Created TAR output in " + (System.currentTimeMillis() - curTime)); return MessageBuilder.withPayload(tmpTarFile).setHeader("dwBatchUuid", dwBatchUuid) .setHeader("fileSuffix", fileSuffix).setHeader("recordsSent", recordsSent) .setHeader("tempPaths", tempPaths).setHeader("dwConfigType", dwConfigType).build(); }
From source file:org.savantbuild.io.tar.TarBuilder.java
/** * Builds the TAR file using the fileSets and Directories provided. * * @return The number of entries added to the TAR file including the directories. * @throws IOException If the build fails. *///from w ww .j a v a 2 s. c o m public int build() throws IOException { if (Files.exists(file)) { Files.delete(file); } if (!Files.isDirectory(file.getParent())) { Files.createDirectories(file.getParent()); } // Sort the file infos and add the directories Set<FileInfo> fileInfos = new TreeSet<>(); for (FileSet fileSet : fileSets) { Set<Directory> dirs = fileSet.toDirectories(); dirs.removeAll(directories); for (Directory dir : dirs) { directories.add(dir); } fileInfos.addAll(fileSet.toFileInfos()); } int count = 0; OutputStream os = Files.newOutputStream(file); try (TarArchiveOutputStream tos = new TarArchiveOutputStream(compress ? new GZIPOutputStream(os) : os)) { tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (Directory directory : directories) { String name = directory.name; TarArchiveEntry entry = new TarArchiveEntry(name.endsWith("/") ? name : name + "/"); if (directory.lastModifiedTime != null) { entry.setModTime(directory.lastModifiedTime.toMillis()); } if (directory.mode != null) { entry.setMode(FileTools.toMode(directory.mode)); } if (storeGroupName && directory.groupName != null) { entry.setGroupName(directory.groupName); } if (storeUserName && directory.userName != null) { entry.setUserName(directory.userName); } tos.putArchiveEntry(entry); tos.closeArchiveEntry(); count++; } for (FileInfo fileInfo : fileInfos) { TarArchiveEntry entry = new TarArchiveEntry(fileInfo.relative.toString()); entry.setModTime(fileInfo.lastModifiedTime.toMillis()); if (storeGroupName) { entry.setGroupName(fileInfo.groupName); } if (storeUserName) { entry.setUserName(fileInfo.userName); } entry.setSize(fileInfo.size); entry.setMode(fileInfo.toMode()); tos.putArchiveEntry(entry); Files.copy(fileInfo.origin, tos); tos.closeArchiveEntry(); count++; } } return count; }
From source file:org.seadva.archive.impl.cloud.SdaArchiveStore.java
public void createTar(final File dir, final String tarFileName) { try {//from w w w . ja v a2 s . co m OutputStream tarOutput = new FileOutputStream(new File(tarFileName)); ArchiveOutputStream tarArchive = new TarArchiveOutputStream(tarOutput); List<File> files = new ArrayList<File>(); File[] filesList = dir.listFiles(); if (filesList != null) { for (File file : filesList) { files.addAll(recurseDirectory(file)); } } for (File file : files) { // tarArchiveEntry = new TarArchiveEntry(file, file.getPath()); TarArchiveEntry tarArchiveEntry = new TarArchiveEntry( file.toString().substring(dir.getAbsolutePath().length() + 1, file.toString().length())); tarArchiveEntry.setSize(file.length()); tarArchive.putArchiveEntry(tarArchiveEntry); FileInputStream fileInputStream = new FileInputStream(file); IOUtils.copy(fileInputStream, tarArchive); fileInputStream.close(); tarArchive.closeArchiveEntry(); } tarArchive.finish(); tarOutput.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.seasr.meandre.components.tools.io.WriteArchive.java
@Override public void executeCallBack(ComponentContext cc) throws Exception { componentInputCache.storeIfAvailable(cc, IN_LOCATION); componentInputCache.storeIfAvailable(cc, IN_FILE_NAME); componentInputCache.storeIfAvailable(cc, IN_DATA); if (archiveStream == null && componentInputCache.hasData(IN_LOCATION)) { Object input = componentInputCache.retrieveNext(IN_LOCATION); if (input instanceof StreamDelimiter) throw new ComponentExecutionException( String.format("Stream delimiters should not arrive on port '%s'!", IN_LOCATION)); String location = DataTypeParser.parseAsString(input)[0]; if (appendExtension) location += String.format(".%s", archiveFormat); outputFile = getLocation(location, defaultFolder); File parentDir = outputFile.getParentFile(); if (!parentDir.exists()) { if (parentDir.mkdirs()) console.finer("Created directory: " + parentDir); } else if (!parentDir.isDirectory()) throw new IOException(parentDir.toString() + " must be a directory!"); if (appendTimestamp) { String name = outputFile.getName(); String timestamp = new SimpleDateFormat(timestampFormat).format(new Date()); int pos = name.lastIndexOf("."); if (pos < 0) name += "_" + timestamp; else// w w w . ja v a 2 s .co m name = String.format("%s_%s%s", name.substring(0, pos), timestamp, name.substring(pos)); outputFile = new File(parentDir, name); } console.fine(String.format("Writing file %s", outputFile)); if (archiveFormat.equals("zip")) { archiveStream = new ZipArchiveOutputStream(outputFile); ((ZipArchiveOutputStream) archiveStream).setLevel(Deflater.BEST_COMPRESSION); } else if (archiveFormat.equals("tar") || archiveFormat.equals("tgz")) { OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(outputFile)); if (archiveFormat.equals("tgz")) fileStream = new GzipCompressorOutputStream(fileStream); archiveStream = new TarArchiveOutputStream(fileStream); } } // Return if we haven't received a zip or tar location yet if (archiveStream == null) return; while (componentInputCache.hasDataAll(new String[] { IN_FILE_NAME, IN_DATA })) { Object inFileName = componentInputCache.retrieveNext(IN_FILE_NAME); Object inData = componentInputCache.retrieveNext(IN_DATA); // check for StreamInitiator if (inFileName instanceof StreamInitiator || inData instanceof StreamInitiator) { if (inFileName instanceof StreamInitiator && inData instanceof StreamInitiator) { StreamInitiator siFileName = (StreamInitiator) inFileName; StreamInitiator siData = (StreamInitiator) inData; if (siFileName.getStreamId() != siData.getStreamId()) throw new ComponentExecutionException("Unequal stream ids received!!!"); if (siFileName.getStreamId() == streamId) isStreaming = true; else // Forward the delimiter(s) cc.pushDataComponentToOutput(OUT_LOCATION, siFileName); continue; } else throw new ComponentExecutionException("Unbalanced StreamDelimiter received!"); } // check for StreamTerminator if (inFileName instanceof StreamTerminator || inData instanceof StreamTerminator) { if (inFileName instanceof StreamTerminator && inData instanceof StreamTerminator) { StreamTerminator stFileName = (StreamTerminator) inFileName; StreamTerminator stData = (StreamTerminator) inData; if (stFileName.getStreamId() != stData.getStreamId()) throw new ComponentExecutionException("Unequal stream ids received!!!"); if (stFileName.getStreamId() == streamId) { // end of stream reached closeArchiveAndPushOutput(); isStreaming = false; break; } else { // Forward the delimiter(s) if (isStreaming) console.warning( "Likely streaming error - received StreamTerminator for a different stream id than the current active stream! - forwarding it"); cc.pushDataComponentToOutput(OUT_LOCATION, stFileName); continue; } } else throw new ComponentExecutionException("Unbalanced StreamDelimiter received!"); } byte[] entryData = null; if (inData instanceof byte[] || inData instanceof Bytes) entryData = DataTypeParser.parseAsByteArray(inData); else if (inData instanceof Document) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DOMUtils.writeXML((Document) inData, baos, outputProperties); entryData = baos.toByteArray(); } else entryData = DataTypeParser.parseAsString(inData)[0].getBytes("UTF-8"); String entryName = DataTypeParser.parseAsString(inFileName)[0]; console.fine(String.format("Adding %s entry: %s", archiveFormat.toUpperCase(), entryName)); ArchiveEntry entry = null; if (archiveFormat.equals("zip")) entry = new ZipArchiveEntry(entryName); else if (archiveFormat.equals("tar") || archiveFormat.equals("tgz")) { entry = new TarArchiveEntry(entryName); ((TarArchiveEntry) entry).setSize(entryData.length); } archiveStream.putArchiveEntry(entry); archiveStream.write(entryData); archiveStream.closeArchiveEntry(); if (!isStreaming) { closeArchiveAndPushOutput(); break; } } }
From source file:org.slc.sli.bulk.extract.files.ExtractFile.java
/** * Generates the archive file for the extract. * * @return//from w ww. ja va 2 s . c om * True on success; otherwise False */ public boolean generateArchive() { boolean success = true; TarArchiveOutputStream tarArchiveOutputStream = null; MultiOutputStream multiOutputStream = new MultiOutputStream(); try { for (String app : clientKeys.keySet()) { SecurityEvent event = securityEventUtil.createSecurityEvent(this.getClass().getName(), "Writing extract file to the file system", LogLevelType.TYPE_INFO, app, BEMessageCode.BE_SE_CODE_0022, app); event.setTargetEdOrgList(edorg); //@TA10431 - change targetEdOrg from scalar to list audit(event); multiOutputStream.addStream(getAppStream(app)); } tarArchiveOutputStream = new TarArchiveOutputStream(multiOutputStream); archiveFile(tarArchiveOutputStream, manifestFile.getFile()); File errors = errorFile.getFile(); if (errors != null) { archiveFile(tarArchiveOutputStream, errors); } for (JsonFileWriter dataFile : dataFiles.values()) { File df = dataFile.getFile(); if (df != null && df.exists()) { archiveFile(tarArchiveOutputStream, df); } } } catch (Exception e) { SecurityEvent event = securityEventUtil.createSecurityEvent(this.getClass().getName(), "Writing extract file to the file system", LogLevelType.TYPE_ERROR, BEMessageCode.BE_SE_CODE_0023); event.setTargetEdOrgList(edorg); //@TA10431 - change targetEdOrg from scalar to list audit(event); LOG.error("Error writing to tar file: {}", e.getMessage()); success = false; for (File archiveFile : archiveFiles.values()) { FileUtils.deleteQuietly(archiveFile); } } finally { IOUtils.closeQuietly(tarArchiveOutputStream); FileUtils.deleteQuietly(tempDir); } return success; }