List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry TarArchiveEntry
public TarArchiveEntry(byte[] headerBuf)
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(); }/*from w w w .j a v a 2 s .co m*/ results.add(packagePath); } return results; }
From source file:org.ngrinder.common.util.CompressionUtils.java
/** * Add a folder into tar.//from w w w . j a v a 2s. c om * * @param tarStream TarArchive outputStream * @param path path to append * @throws IOException thrown when having IO problem. */ public static void addFolderToTar(TarArchiveOutputStream tarStream, String path) throws IOException { TarArchiveEntry archiveEntry = new TarArchiveEntry(path); archiveEntry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE); tarStream.putArchiveEntry(archiveEntry); tarStream.closeArchiveEntry(); }
From source file:org.ngrinder.common.util.CompressionUtils.java
/** * Add the given input stream into tar./* w ww . j a va2 s . c o m*/ * * @param tarStream TarArchive outputStream * @param inputStream input stream * @param path relative path to append * @param size size of stream * @param mode mode for this entry * @throws IOException thrown when having IO problem. */ public static void addInputStreamToTar(TarArchiveOutputStream tarStream, InputStream inputStream, String path, long size, int mode) throws IOException { TarArchiveEntry entry = new TarArchiveEntry(path); entry.setSize(size); entry.setMode(mode); try { tarStream.putArchiveEntry(entry); IOUtils.copy(inputStream, tarStream); } catch (IOException e) { throw processException("Error while adding File to Tar file", e); } finally { tarStream.closeArchiveEntry(); } }
From source file:org.ngrinder.common.util.CompressionUtils.java
/** * Add a file into tar.//from w w w .ja va2s.c o m * * @param tarStream TarArchive outputStream * @param file file * @param path relative path to append * @param mode mode for this entry * @throws IOException thrown when having IO problem. */ public static void addFileToTar(TarArchiveOutputStream tarStream, File file, String path, int mode) throws IOException { TarArchiveEntry entry = new TarArchiveEntry(path); entry.setSize(file.length()); entry.setMode(mode); BufferedInputStream bis = null; try { tarStream.putArchiveEntry(entry); bis = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(bis, tarStream); } catch (IOException e) { throw processException("Error while adding File to Tar file", e); } finally { IOUtils.closeQuietly(bis); tarStream.closeArchiveEntry(); } }
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. * // w w w.j a v a 2 s.c om * @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 va 2 s .c om*/ 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 {// ww w. j a va 2s. c om 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 . j a va2 s . c o 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.soulwing.credo.service.archive.TarGzipArchiveBuilder.java
@Override protected void onBeginEntry(String name, String charset) throws IOException { entry = new TarArchiveEntry(name); entry.setIds(USER_ID, GROUP_ID);//from w w w .j av a 2 s . c o m entry.setNames(USER_NAME, GROUP_NAME); entry.setMode(MODE); }
From source file:org.torproject.collector.bridgedescs.TarballBuilder.java
/** Writes the previously configured tarball with all contained files to the * given file, or fail if the file extension is not known. */ void build(File directory) throws IOException { File tarballFile = new File(directory, this.tarballFileName); TarArchiveOutputStream taos = null;/*w w w .j a v a2 s. c o m*/ if (this.tarballFileName.endsWith(".tar.gz")) { taos = new TarArchiveOutputStream( new GzipCompressorOutputStream(new BufferedOutputStream(new FileOutputStream(tarballFile)))); } else if (this.tarballFileName.endsWith(".tar.bz2")) { taos = new TarArchiveOutputStream( new BZip2CompressorOutputStream(new BufferedOutputStream(new FileOutputStream(tarballFile)))); } else if (this.tarballFileName.endsWith(".tar")) { taos = new TarArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(tarballFile))); } else { fail("Unknown file extension: " + this.tarballFileName); } for (Map.Entry<String, TarballFile> file : this.tarballFiles.entrySet()) { TarArchiveEntry tae = new TarArchiveEntry(file.getKey()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (DescriptorBuilder descriptorBuilder : file.getValue().descriptorBuilders) { descriptorBuilder.build(baos); } tae.setSize(baos.size()); tae.setModTime(file.getValue().modifiedMillis); taos.putArchiveEntry(tae); taos.write(baos.toByteArray()); taos.closeArchiveEntry(); } taos.close(); tarballFile.setLastModified(this.modifiedMillis); }