List of usage examples for org.apache.commons.compress.archivers ArchiveOutputStream closeArchiveEntry
public abstract void closeArchiveEntry() throws IOException;
From source file:com.amazonaws.codepipeline.jenkinsplugin.CompressionTools.java
private static void compressArchive(final Path pathToCompress, final ArchiveOutputStream archiveOutputStream, final ArchiveEntryFactory archiveEntryFactory, final CompressionType compressionType, final BuildListener listener) throws IOException { final List<File> files = addFilesToCompress(pathToCompress, listener); LoggingHelper.log(listener, "Compressing directory '%s' as a '%s' archive", pathToCompress.toString(), compressionType.name());/*from w ww.j a v a 2 s. c o m*/ for (final File file : files) { final String newTarFileName = pathToCompress.relativize(file.toPath()).toString(); final ArchiveEntry archiveEntry = archiveEntryFactory.create(file, newTarFileName); archiveOutputStream.putArchiveEntry(archiveEntry); try (final FileInputStream fileInputStream = new FileInputStream(file)) { IOUtils.copy(fileInputStream, archiveOutputStream); } archiveOutputStream.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. jav a 2s . com*/ entry.setIds(0, 0); entry.setNames("vvrte", "cpcc"); os.putArchiveEntry(entry); os.write(content); os.closeArchiveEntry(); }
From source file:com.openkm.misc.ZipTest.java
public void testApache() throws IOException, ArchiveException { log.debug("testApache()"); File zip = File.createTempFile("apache_", ".zip"); // Create zip FileOutputStream fos = new FileOutputStream(zip); ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream("zip", fos); aos.putArchiveEntry(new ZipArchiveEntry("coeta")); aos.closeArchiveEntry(); aos.close();/*from w w w . j a v a 2 s . c om*/ // Read zip FileInputStream fis = new FileInputStream(zip); ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", fis); ZipArchiveEntry zae = (ZipArchiveEntry) ais.getNextEntry(); assertEquals(zae.getName(), "coeta"); ais.close(); }
From source file:com.fizzed.stork.deploy.Archive.java
static public void packEntry(ArchiveOutputStream aos, Path dirOrFile, String base, boolean appendName) throws IOException { String entryName = base;/*from w ww. j a v a2s. c om*/ if (appendName) { if (!entryName.equals("")) { if (!entryName.endsWith("/")) { entryName += "/" + dirOrFile.getFileName(); } else { entryName += dirOrFile.getFileName(); } } else { entryName += dirOrFile.getFileName(); } } ArchiveEntry entry = aos.createArchiveEntry(dirOrFile.toFile(), entryName); if (Files.isRegularFile(dirOrFile)) { if (entry instanceof TarArchiveEntry && Files.isExecutable(dirOrFile)) { // -rwxr-xr-x ((TarArchiveEntry) entry).setMode(493); } else { // keep default mode } } aos.putArchiveEntry(entry); if (Files.isRegularFile(dirOrFile)) { Files.copy(dirOrFile, aos); aos.closeArchiveEntry(); } else { aos.closeArchiveEntry(); List<Path> children = Files.list(dirOrFile).collect(Collectors.toList()); if (children != null) { for (Path childFile : children) { packEntry(aos, childFile, entryName + "/", true); } } } }
From source file:consulo.cold.runner.execute.target.artifacts.Generator.java
private static void copyEntry(ArchiveOutputStream archiveOutputStream, ArchiveInputStream ais, ArchiveEntry tempEntry, ArchiveEntryWrapper newEntry) throws IOException { byte[] data = null; if (!tempEntry.isDirectory()) { try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) { IOUtils.copy(ais, byteStream); data = byteStream.toByteArray(); }//from www .jav a 2 s. c om // change line breaks try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { if (LineEnds.convert(new ByteArrayInputStream(data), stream, LineEnds.STYLE_UNIX)) { data = stream.toByteArray(); } } catch (BinaryDataException ignored) { // ignore binary data } } if (data != null) { newEntry.setSize(data.length); } archiveOutputStream.putArchiveEntry(newEntry.getArchiveEntry()); if (data != null) { IOUtils.copy(new ByteArrayInputStream(data), archiveOutputStream); } archiveOutputStream.closeArchiveEntry(); }
From source file:jetbrains.exodus.util.CompressBackupUtil.java
/** * Adds the file to the tar archive represented by output stream. It's caller's responsibility to close output stream * properly./* w w w.j a v a2 s.c o m*/ * * @param out target archive. * @param pathInArchive relative path in archive. It will lead the name of the file in the archive. * @param source file to be added. * @param fileSize size of the file (which is known in most cases). * @throws IOException in case of any issues with underlying store. */ public static void archiveFile(@NotNull final ArchiveOutputStream out, @NotNull final String pathInArchive, @NotNull final File source, final long fileSize) throws IOException { if (!source.isFile()) { throw new IllegalArgumentException("Provided source is not a file: " + source.getAbsolutePath()); } //noinspection ChainOfInstanceofChecks if (out instanceof TarArchiveOutputStream) { final TarArchiveEntry entry = new TarArchiveEntry(pathInArchive + source.getName()); entry.setSize(fileSize); entry.setModTime(source.lastModified()); out.putArchiveEntry(entry); } else if (out instanceof ZipArchiveOutputStream) { final ZipArchiveEntry entry = new ZipArchiveEntry(pathInArchive + source.getName()); entry.setSize(fileSize); entry.setTime(source.lastModified()); out.putArchiveEntry(entry); } else { throw new IOException("Unknown archive output stream"); } try (InputStream input = new FileInputStream(source)) { IOUtil.copyStreams(input, fileSize, out, IOUtil.BUFFER_ALLOCATOR); } out.closeArchiveEntry(); }
From source file:gov.nih.nci.caarray.application.fileaccess.FileAccessUtils.java
/** * Add a file to an archive./*from w ww . j a v a 2s .c o m*/ * * @param f the file to add. * @param zout the archive stream. {@link TarArchiveOutputStream} or {@link ZipArchiveOutputStream} * @throws IOException when writeing to the stream fails. */ public void addFileToArchive(CaArrayFile f, ArchiveOutputStream zout) throws IOException { final ArchiveEntry ae = createArchiveEntry(zout, f.getName(), f.getUncompressedSize()); zout.putArchiveEntry(ae); this.dataStorageFacade.copyDataToStream(f.getDataHandle(), zout); zout.closeArchiveEntry(); }
From source file:fr.gael.ccsds.sip.archive.TarArchiveManager.java
/** * Produces TAR archive//from w ww . j av a 2 s .com */ @Override public File copy(final File src, final File tar_file, final String dst) throws Exception { final ArchiveStreamFactory asf = new ArchiveStreamFactory(); // Case of tar already exist: all the entries must be copied.. if (tar_file.exists()) { final FileInputStream fis = new FileInputStream(tar_file); final ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis); final File tempFile = File.createTempFile("updateTar", "tar"); final FileOutputStream fos = new FileOutputStream(tempFile); final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = ais.getNextEntry()) != null) { aos.putArchiveEntry(nextEntry); IOUtils.copy(ais, aos); aos.closeArchiveEntry(); } // create the new entry final TarArchiveEntry entry = new TarArchiveEntry(src, dst); entry.setSize(src.length()); aos.putArchiveEntry(entry); final FileInputStream sfis = new FileInputStream(src); IOUtils.copy(sfis, aos); sfis.close(); aos.closeArchiveEntry(); aos.finish(); ais.close(); aos.close(); fis.close(); // copies the new file over the old tar_file.delete(); tempFile.renameTo(tar_file); return tar_file; } else { final FileOutputStream fos = new FileOutputStream(tar_file); final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos); // create the new entry final TarArchiveEntry entry = new TarArchiveEntry(src, dst); entry.setSize(src.length()); aos.putArchiveEntry(entry); final FileInputStream sfis = new FileInputStream(src); IOUtils.copy(sfis, aos); sfis.close(); aos.closeArchiveEntry(); aos.finish(); aos.close(); fos.close(); } return tar_file; }
From source file:ch.rasc.embeddedtc.plugin.PackageTcWarMojo.java
private void addFile(ArchiveOutputStream aos, String from, String to) throws IOException { aos.putArchiveEntry(new JarArchiveEntry(to)); IOUtils.copy(getClass().getResourceAsStream(from), aos); aos.closeArchiveEntry(); }
From source file:fr.gael.ccsds.sip.archive.TgzArchiveManager.java
@Override public File copy(final File src, final File tar_file, final String dst) throws Exception { final ArchiveStreamFactory asf = new ArchiveStreamFactory(); final CompressorStreamFactory csf = new CompressorStreamFactory(); // Case of tar already exist: all the entries must be copied.. if (tar_file.exists()) { final FileInputStream fis = new FileInputStream(tar_file); final CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP, fis); final ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis); final File tempFile = File.createTempFile("updateTar", "tar"); final FileOutputStream fos = new FileOutputStream(tempFile); final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos); final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos); // copy the existing entries ArchiveEntry nextEntry;// w w w. ja va 2 s . co m while ((nextEntry = ais.getNextEntry()) != null) { aos.putArchiveEntry(nextEntry); IOUtils.copy(ais, aos); aos.closeArchiveEntry(); } // create the new entry final TarArchiveEntry entry = new TarArchiveEntry(src, dst); entry.setSize(src.length()); aos.putArchiveEntry(entry); final FileInputStream sfis = new FileInputStream(src); IOUtils.copy(sfis, aos); sfis.close(); aos.closeArchiveEntry(); aos.finish(); ais.close(); aos.close(); fis.close(); // copies the new file over the old tar_file.delete(); tempFile.renameTo(tar_file); return tar_file; } else { final FileOutputStream fos = new FileOutputStream(tar_file); final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos); final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos); // create the new entry final TarArchiveEntry entry = new TarArchiveEntry(src, dst); entry.setSize(src.length()); aos.putArchiveEntry(entry); final FileInputStream sfis = new FileInputStream(src); IOUtils.copy(sfis, aos); sfis.close(); aos.closeArchiveEntry(); aos.finish(); aos.close(); fos.close(); } return tar_file; }