List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry TarArchiveEntry
public TarArchiveEntry(byte[] headerBuf)
From source file:com.facebook.buck.util.unarchive.UntarTest.java
/** * cleanDirectoriesExactly asserts that OVERWRITE_AND_CLEAN_DIRECTORIES removes exactly the files * in any subdirectory of a directory entry in the tar archive. * * <p>This behavior supports unarchiving a Buck cache from multiple archives, each containing a * part.//from w w w .j a v a 2 s .c o m * * <p>Explanations: * <li>BUCK isn't removed because it's not in a subdirectory of a directory entry in the archive. * <li>buck-out/gen/pkg1/rule1.jar isn't removed, even though buck-out/gen/pkg1/rule2.jar is in * the archive, because there's no directory entry for buck-out/gen/pkg1/. * <li>buck-out/gen/pkg1/rule2#foo/lib.so, however, is removed because the archive contains the * directory buck-out/gen/pkg1/rule2#foo/ */ @Test public void cleanDirectoriesExactly() throws Exception { Path archive = filesystem.resolve("archive.tar"); Path outDir = filesystem.getRootPath(); List<String> toLeave = ImmutableList.of("BUCK", "buck-out/gen/pkg1/rule1.jar", "buck-out/gen/pkg1/rule1#foo/lib.so", "buck-out/gen/pkg2/rule.jar", "buck-out/gen/pkg2/rule#foo/lib.so"); List<String> toDelete = ImmutableList.of("buck-out/gen/pkg1/rule2#foo/lib.so"); for (String s : concat(toDelete, toLeave)) { Path path = filesystem.resolve(s); filesystem.createParentDirs(path); filesystem.writeContentsToPath("", path); } // Write test archive. try (TarArchiveOutputStream stream = new TarArchiveOutputStream( new BufferedOutputStream(filesystem.newFileOutputStream(archive)))) { stream.putArchiveEntry(new TarArchiveEntry("buck-out/gen/pkg1/rule2#foo/")); stream.putArchiveEntry(new TarArchiveEntry("buck-out/gen/pkg1/rule2.jar")); stream.closeArchiveEntry(); } // Untar test archive. Untar.tarUnarchiver().extractArchive(archive, filesystem, outDir, Optional.empty(), ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES); // Assert expected file existence. for (String s : toDelete) { Assert.assertFalse(String.format("Expected file %s to be deleted, but it wasn't", s), filesystem.exists(filesystem.resolve(s))); } for (String s : toLeave) { Assert.assertTrue(String.format("Expected file %s to not be deleted, but it was", s), filesystem.exists(filesystem.resolve(s))); } }
From source file:gov.nih.nci.ncicb.tcga.dcc.dam.processors.FilePackager.java
void makeArchive(final String readmeTempFilename) throws IOException { final byte[] buffer = new byte[1024]; File archiveFile = null;/*ww w . j av a2 s . co m*/ TarArchiveOutputStream out = null; //in case we need to write to an external server final String archiveName = prefixPathForExternalServer( filePackagerBean.getArchivePhysicalName() + ".tar.gz"); try { archiveFile = new File(archiveName); out = makeTarGzOutputStream(archiveFile); copyManifestToArchive(out); copyReadmeToArchive(out, readmeTempFilename); int i = 0; for (final DataFile fileInfo : filePackagerBean.getSelectedFiles()) { final File file = new File(fileInfo.getPath()); if (!file.exists()) { throw new IOException("Data file does not exist: " + fileInfo.getPath()); } logger.logToLogger(Level.DEBUG, "tarring file " + (++i) + ":" + fileInfo.getPath() + " into " + archiveName); //"synthetic" file path, as we want it to appear in the tar final String archiveFilePath = constructInternalFilePath(fileInfo); final TarArchiveEntry tarAdd = new TarArchiveEntry(file); tarAdd.setModTime(file.lastModified()); tarAdd.setName(archiveFilePath); out.putArchiveEntry(tarAdd); FileInputStream in = null; try { in = new FileInputStream(file); int nRead = in.read(buffer, 0, buffer.length); while (nRead >= 0) { out.write(buffer, 0, nRead); nRead = in.read(buffer, 0, buffer.length); } } finally { if (in != null) { in.close(); } } out.closeArchiveEntry(); if (fileInfo.getCacheFileToGenerate() != null) { //a special case where there should be a cache file but it doesn't exist - // Send email with error message //filePackagerFactory.getErrorMailSender().send(Messages.CACHE_ERROR, MessageFormat.format(Messages.CACHE_FILE_NOT_FOUND, fileInfo.getCacheFileToGenerate())); } } } catch (IOException ex) { //delete the out file if it exists if (out != null) { out.close(); out = null; } if (archiveFile != null && archiveFile.exists()) { // give OS time to delete file handle try { Thread.sleep(100); } catch (InterruptedException ie) { // it's ok } // keep track of uncompressed size this.actualUncompressedSize = archiveFile.length(); //noinspection ResultOfMethodCallIgnored archiveFile.delete(); } throw ex; } finally { if (out != null) { out.close(); } } logger.logToLogger(Level.DEBUG, "Created tar " + archiveName); }
From source file:gov.nih.nci.ncicb.tcga.dcc.dam.processors.FilePackager.java
private void copyFileToArchive(final TarArchiveOutputStream out, final String tempFilename, final String filename) throws IOException { if (StringUtils.isEmpty(tempFilename)) { return;/* w ww . j a v a2s . c o m*/ } final byte[] buffer = new byte[1024]; final File file = new File(tempFilename); if (!file.exists()) { throw new IOException("File does not exist: " + tempFilename); } final TarArchiveEntry tarAdd = new TarArchiveEntry(file); tarAdd.setModTime(file.lastModified()); tarAdd.setName(filename); out.putArchiveEntry(tarAdd); FileInputStream in = null; try { in = new FileInputStream(file); int nRead = in.read(buffer, 0, buffer.length); while (nRead >= 0) { out.write(buffer, 0, nRead); nRead = in.read(buffer, 0, buffer.length); } } finally { if (in != null) { in.close(); } } out.closeArchiveEntry(); }
From source file:cpcc.vvrte.services.VirtualVehicleMigratorTest.java
private static void appendEntryToStream(String entryName, byte[] content, ArchiveOutputStream os) throws IOException { TarArchiveEntry entry = new TarArchiveEntry(entryName); entry.setModTime(new Date()); entry.setSize(content.length);// ww w .java 2 s . c o m entry.setIds(0, 0); entry.setNames("vvrte", "cpcc"); os.putArchiveEntry(entry); os.write(content); os.closeArchiveEntry(); }
From source file:gov.noaa.pfel.coastwatch.util.FileVisitorDNLS.java
/** * This makes a .tgz or .tar.gz file./*from www .j a v a2 s .co m*/ * * @param tResultName is the full result file name, usually * the name of the dir being archived, and ending in .tgz or .tar.gz. */ public static void makeTgz(String tDir, String tFileNameRegex, boolean tRecursive, String tPathRegex, String tResultName) throws Exception { TarArchiveOutputStream tar = null; String outerDir = File2.getDirectory(tDir.substring(0, tDir.length() - 1)); tar = new TarArchiveOutputStream( new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(tResultName)))); // Add data to out and flush stream Table filesTable = oneStep(tDir, tFileNameRegex, tRecursive, tPathRegex, false); //tDirectoriesToo StringArray directoryPA = (StringArray) filesTable.getColumn(DIRECTORY); StringArray namePA = (StringArray) filesTable.getColumn(NAME); LongArray lastModifiedPA = (LongArray) filesTable.getColumn(LASTMODIFIED); LongArray sizePA = (LongArray) filesTable.getColumn(SIZE); byte buffer[] = new byte[32768]; int nBytes; for (int fi = 0; fi < namePA.size(); fi++) { String fullName = directoryPA.get(fi) + namePA.get(fi); TarArchiveEntry entry = new TarArchiveEntry(new File(fullName.substring(outerDir.length()))); entry.setSize(sizePA.get(fi)); entry.setModTime(lastModifiedPA.get(fi)); tar.putArchiveEntry(entry); FileInputStream fis = new FileInputStream(fullName); while ((nBytes = fis.read(buffer)) > 0) tar.write(buffer, 0, nBytes); fis.close(); tar.closeArchiveEntry(); } tar.close(); }
From source file:com.facebook.buck.core.build.engine.impl.CachingBuildEngineTest.java
private static void writeEntriesToArchive(Path file, ImmutableMap<Path, String> entries, ImmutableList<Path> directories) throws IOException { try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(file)); OutputStream z = new ZstdCompressorOutputStream(o); TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) { archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); for (Map.Entry<Path, String> mapEntry : entries.entrySet()) { TarArchiveEntry e = new TarArchiveEntry(mapEntry.getKey().toString()); e.setModTime(ZipConstants.getFakeTime()); byte[] bytes = mapEntry.getValue().getBytes(UTF_8); e.setSize(bytes.length);//w w w.jav a 2 s . c o m archive.putArchiveEntry(e); archive.write(bytes); archive.closeArchiveEntry(); } for (Path dir : directories) { TarArchiveEntry e = new TarArchiveEntry(dir.toString() + "/"); e.setModTime(ZipConstants.getFakeTime()); } archive.finish(); } }
From source file:org.apache.camel.dataformat.tarfile.TarFileDataFormat.java
@Override public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { String filename = exchange.getIn().getHeader(FILE_NAME, String.class); Long filelength = exchange.getIn().getHeader(FILE_LENGTH, Long.class); if (filename != null) { filename = new File(filename).getName(); // remove any path elements } else {/* ww w . ja v a 2 s . c om*/ // generate the file name as the camel file component would do filename = StringHelper.sanitize(exchange.getIn().getMessageId()); } TarArchiveOutputStream tos = new TarArchiveOutputStream(stream); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, graph); if (filelength == null) { filelength = new Long(is.available()); } TarArchiveEntry entry = new TarArchiveEntry(filename); entry.setSize(filelength); tos.putArchiveEntry(entry); try { IOHelper.copy(is, tos); } finally { tos.closeArchiveEntry(); IOHelper.close(is, tos); } String newFilename = filename + ".tar"; exchange.getOut().setHeader(FILE_NAME, newFilename); }
From source file:org.apache.camel.dataformat.tarfile.TarFileDataFormatTest.java
private static byte[] getTaredText(String entryName) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(TEXT.getBytes("UTF-8")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); TarArchiveOutputStream tos = new TarArchiveOutputStream(baos); try {//from w ww. j a va2 s .c o m TarArchiveEntry entry = new TarArchiveEntry(entryName); entry.setSize(bais.available()); tos.putArchiveEntry(entry); IOHelper.copy(bais, tos); } finally { tos.closeArchiveEntry(); IOHelper.close(bais, tos); } return baos.toByteArray(); }
From source file:org.apache.camel.processor.aggregate.TarAggregationStrategy.java
@Override public void onCompletion(Exchange exchange) { List<Exchange> list = exchange.getProperty(Exchange.GROUPED_EXCHANGE, List.class); try {/* w w w. jav a 2s. c o m*/ ByteArrayOutputStream bout = new ByteArrayOutputStream(); TarArchiveOutputStream tout = new TarArchiveOutputStream(bout); for (Exchange item : list) { String name = item.getProperty(TAR_ENTRY_NAME, item.getProperty(Exchange.FILE_NAME, item.getExchangeId(), String.class), String.class); byte[] body = item.getIn().getBody(byte[].class); TarArchiveEntry entry = new TarArchiveEntry(name); entry.setSize(body.length); tout.putArchiveEntry(entry); tout.write(body); tout.closeArchiveEntry(); } tout.close(); exchange.getIn().setBody(bout.toByteArray()); exchange.removeProperty(Exchange.GROUPED_EXCHANGE); } catch (Exception e) { throw new RuntimeException("Unable to tar exchanges!", e); } }
From source file:org.apache.camel.processor.aggregate.tarfile.TarAggregationStrategy.java
private static void addFileToTar(File source, File file, String fileName) throws IOException, ArchiveException { File tmpTar = File.createTempFile(source.getName(), null); tmpTar.delete();//from www . j av a2 s . com if (!source.renameTo(tmpTar)) { throw new IOException("Could not make temp file (" + source.getName() + ")"); } TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, new FileInputStream(tmpTar)); TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); InputStream in = new FileInputStream(file); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = tin.getNextEntry()) != null) { tos.putArchiveEntry(nextEntry); IOUtils.copy(tin, tos); tos.closeArchiveEntry(); } // Add the new entry TarArchiveEntry entry = new TarArchiveEntry(fileName == null ? file.getName() : fileName); entry.setSize(file.length()); tos.putArchiveEntry(entry); IOUtils.copy(in, tos); tos.closeArchiveEntry(); IOHelper.close(in); IOHelper.close(tin); IOHelper.close(tos); }