List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry setModTime
public void setModTime(Date time)
From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java
/** * @param virtualVehicle the virtual vehicle. * @param os the output stream to write to. * @param chunkNumber the chunk number./*from w ww . jav a 2 s .c o m*/ * @throws IOException thrown in case of errors. */ private void writeVirtualVehicleProperties(VirtualVehicle virtualVehicle, ArchiveOutputStream os, int chunkNumber, boolean lastChunk) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Properties virtualVehicleProps = fillVirtualVehicleProps(virtualVehicle, lastChunk); virtualVehicleProps.store(baos, "Virtual Vehicle Properties"); baos.close(); byte[] propBytes = baos.toByteArray(); TarArchiveEntry entry = new TarArchiveEntry(DATA_VV_PROPERTIES); entry.setModTime(new Date()); entry.setSize(propBytes.length); entry.setIds(0, chunkNumber); entry.setNames("vvrte", "cpcc"); os.putArchiveEntry(entry); os.write(propBytes); os.closeArchiveEntry(); }
From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java
/** * @param virtualVehicle the virtual vehicle. * @param os the output stream to write to. * @param chunkNumber the chunk number./* w w w.j a v a 2 s .c o m*/ * @throws IOException thrown in case of errors. */ private void writeVirtualVehicleContinuation(VirtualVehicle virtualVehicle, ArchiveOutputStream os, int chunkNumber) throws IOException { byte[] continuation = virtualVehicle.getContinuation(); if (continuation == null) { return; } TarArchiveEntry entry = new TarArchiveEntry(DATA_VV_CONTINUATION_JS); entry.setModTime(new Date()); entry.setSize(continuation.length); entry.setIds(0, chunkNumber); entry.setNames("vvrte", "cpcc"); os.putArchiveEntry(entry); os.write(continuation); os.closeArchiveEntry(); }
From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java
/** * @param os the output stream to write to. * @throws IOException thrown in case of errors. *///from w w w .ja va 2 s .com private void writeVirtualVehicleStorageChunk(VirtualVehicle virtualVehicle, ArchiveOutputStream os, int chunkNumber, List<VirtualVehicleStorage> storageChunk) throws IOException { for (VirtualVehicleStorage se : storageChunk) { logger.debug("Writing storage entry '" + se.getName() + "'"); byte[] content = se.getContentAsByteArray(); TarArchiveEntry entry = new TarArchiveEntry("storage/" + se.getName()); entry.setModTime(se.getModificationTime()); entry.setSize(content.length); entry.setIds(se.getId(), chunkNumber); entry.setNames("vvrte", "cpcc"); os.putArchiveEntry(entry); os.write(content); os.closeArchiveEntry(); } }
From source file:edu.mit.lib.bagit.Filler.java
private void fillArchive(File dirFile, String relBase, ArchiveOutputStream out) throws IOException { for (File file : dirFile.listFiles()) { String relPath = relBase + File.separator + file.getName(); if (file.isDirectory()) { fillArchive(file, relPath, out); } else {/*from www . ja v a2s . c o m*/ TarArchiveEntry entry = new TarArchiveEntry(relPath); entry.setSize(file.length()); entry.setModTime(0L); out.putArchiveEntry(entry); Files.copy(file.toPath(), out); out.closeArchiveEntry(); } } }
From source file:freenet.client.async.ContainerInserter.java
/** ** OutputStream os will be close()d if this method returns successfully. *//*from w ww .j a va 2s. c o m*/ private String createTarBucket(OutputStream os) throws IOException { if (logMINOR) Logger.minor(this, "Create a TAR Bucket"); TarArchiveOutputStream tarOS = new TarArchiveOutputStream(os); try { tarOS.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); TarArchiveEntry ze; for (ContainerElement ph : containerItems) { if (logMINOR) Logger.minor(this, "Putting into tar: " + ph + " data length " + ph.data.size() + " name " + ph.targetInArchive); ze = new TarArchiveEntry(ph.targetInArchive); ze.setModTime(0); long size = ph.data.size(); ze.setSize(size); tarOS.putArchiveEntry(ze); BucketTools.copyTo(ph.data, tarOS, size); tarOS.closeArchiveEntry(); } } finally { tarOS.close(); } return ARCHIVE_TYPE.TAR.mimeTypes[0]; }
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;/*from ww w. j a va 2 s.co 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: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;/* w w w . j a v a 2s. 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.noaa.pfel.coastwatch.util.FileVisitorDNLS.java
/** * This makes a .tgz or .tar.gz file.// w w w . j av a 2s . c o 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);// ww w . j av a 2 s .com 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.ant.compress.taskdefs.Tar.java
public Tar() { setFactory(new TarStreamFactory() { public ArchiveOutputStream getArchiveStream(OutputStream stream, String encoding) throws IOException { TarArchiveOutputStream o = (TarArchiveOutputStream) super.getArchiveStream(stream, encoding); if (format.equals(Format.OLDGNU)) { o.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); } else if (format.equals(Format.GNU)) { o.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); o.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); } else if (format.equals(Format.STAR)) { o.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); o.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); } else if (format.equals(Format.PAX)) { o.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); o.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); o.setAddPaxHeadersForNonAsciiNames(true); }/* www.jav a 2 s. co m*/ return o; } }); setEntryBuilder(new ArchiveBase.EntryBuilder() { public ArchiveEntry buildEntry(ArchiveBase.ResourceWithFlags r) { boolean isDir = r.getResource().isDirectory(); String name = r.getName(); if (isDir && !name.endsWith("/")) { name += "/"; } else if (!isDir && name.endsWith("/")) { name = name.substring(0, name.length() - 1); } TarArchiveEntry ent = new TarArchiveEntry(name, getPreserveLeadingSlashes()); ent.setModTime(round(r.getResource().getLastModified(), 1000)); ent.setSize(isDir ? 0 : r.getResource().getSize()); if (!isDir && r.getCollectionFlags().hasModeBeenSet()) { ent.setMode(r.getCollectionFlags().getMode()); } else if (isDir && r.getCollectionFlags().hasDirModeBeenSet()) { ent.setMode(r.getCollectionFlags().getDirMode()); } else if (r.getResourceFlags().hasModeBeenSet()) { ent.setMode(r.getResourceFlags().getMode()); } else { ent.setMode(isDir ? ArchiveFileSet.DEFAULT_DIR_MODE : ArchiveFileSet.DEFAULT_FILE_MODE); } if (r.getResourceFlags().hasUserIdBeenSet()) { ent.setUserId(r.getResourceFlags().getUserId()); } else if (r.getCollectionFlags().hasUserIdBeenSet()) { ent.setUserId(r.getCollectionFlags().getUserId()); } if (r.getResourceFlags().hasGroupIdBeenSet()) { ent.setGroupId(r.getResourceFlags().getGroupId()); } else if (r.getCollectionFlags().hasGroupIdBeenSet()) { ent.setGroupId(r.getCollectionFlags().getGroupId()); } if (r.getResourceFlags().hasUserNameBeenSet()) { ent.setUserName(r.getResourceFlags().getUserName()); } else if (r.getCollectionFlags().hasUserNameBeenSet()) { ent.setUserName(r.getCollectionFlags().getUserName()); } if (r.getResourceFlags().hasGroupNameBeenSet()) { ent.setGroupName(r.getResourceFlags().getGroupName()); } else if (r.getCollectionFlags().hasGroupNameBeenSet()) { ent.setGroupName(r.getCollectionFlags().getGroupName()); } return ent; } }); setFileSetBuilder(new ArchiveBase.FileSetBuilder() { public ArchiveFileSet buildFileSet(Resource dest) { ArchiveFileSet afs = new TarFileSet(); afs.setSrcResource(dest); return afs; } }); }