List of usage examples for org.apache.commons.compress.archivers ArchiveOutputStream closeArchiveEntry
public abstract void closeArchiveEntry() throws IOException;
From source file:de.fischer.thotti.core.distbuilder.CommonsCompressTest.java
public static void main(String[] args) throws IOException, ArchiveException { File output = new File("C:\\projekte\\thotti.master\\test.zip"); File file1 = new File("C:\\projekte\\thotti.master\\pom.xml"); File file2 = new File("C:\\projekte\\thotti.master\\todo.txt"); final OutputStream out = new FileOutputStream(output); ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out); os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml")); IOUtils.copy(new FileInputStream(file1), os); os.closeArchiveEntry(); os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml")); IOUtils.copy(new FileInputStream(file2), os); os.closeArchiveEntry();/*from w w w . j a va 2 s. c o m*/ out.flush(); os.close(); }
From source file:big.zip.java
/** * /*from w w w . j ava 2 s . c o m*/ * @param fileToCompress The file that we want to compress * @param fileToOutput The zip file containing the compressed file * @return True if the file was compressed and created, false when something * went wrong */ public static boolean compress(final File fileToCompress, final File fileToOutput) { if (fileToOutput.exists()) { // do the first run to delete this file fileToOutput.delete(); // did this worked? if (fileToOutput.exists()) { // something went wrong, the file is still here System.out.println("ZIP59 - Failed to delete output file: " + fileToOutput.getAbsolutePath()); return false; } } // does our file to compress exist? if (fileToCompress.exists() == false) { // we have a problem here System.out.println("ZIP66 - Didn't found the file to compress: " + fileToCompress.getAbsolutePath()); return false; } // all checks are done, now it is time to do the compressing try { final OutputStream outputStream = new FileOutputStream(fileToOutput); ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream("zip", outputStream); archive.putArchiveEntry(new ZipArchiveEntry(fileToCompress.getName())); // create the input file stream and copy it over to the archive FileInputStream inputStream = new FileInputStream(fileToCompress); IOUtils.copy(inputStream, archive); // close the archive archive.closeArchiveEntry(); archive.flush(); archive.close(); // now close the input file stream inputStream.close(); // and close the output file stream too outputStream.flush(); outputStream.close(); } catch (FileNotFoundException ex) { Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (ArchiveException ex) { Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (IOException ex) { Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex); return false; } return true; }
From source file:com.chnoumis.commons.zip.utils.ZipUtils.java
public static void zip(List<ZipInfo> zipInfos, OutputStream out) throws IOException, ArchiveException { ArchiveOutputStream os = null; try {//from w ww. ja va 2 s . c o m os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out); for (ZipInfo zipInfo : zipInfos) { os.putArchiveEntry(new ZipArchiveEntry(zipInfo.getFileName())); InputStream o = null; if (zipInfo.getFileContent() != null) { o = new ByteArrayInputStream(zipInfo.getFileContent()); } else { o = zipInfo.getInputStream(); } IOUtils.copy(o, os); os.closeArchiveEntry(); } } finally { if (os != null) { os.close(); } } out.close(); }
From source file:io.zatarox.satellite.impl.BackgroundWrapperTest.java
@BeforeClass public static void setBefore() throws Exception { file = File.createTempFile("archive", ".jar"); file.deleteOnExit();/*from w w w. jav a2s. c om*/ final FileOutputStream out = new FileOutputStream(file); ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, out); ZipArchiveEntry entry = new ZipArchiveEntry("META-INF/MANIFEST.MF"); archive.putArchiveEntry(entry); final Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); manifest.getMainAttributes().putValue("Background-Process-Class", FakeBackgroundProcessImpl.class.getName()); manifest.write(archive); archive.closeArchiveEntry(); archive.close(); }
From source file:ch.rasc.embeddedtc.plugin.PackageTcWarMojo.java
private static void extractJarToArchive(JarFile file, ArchiveOutputStream aos) throws IOException { Enumeration<? extends JarEntry> entries = file.entries(); while (entries.hasMoreElements()) { JarEntry j = entries.nextElement(); if (!"META-INF/MANIFEST.MF".equals(j.getName())) { aos.putArchiveEntry(new JarArchiveEntry(j.getName())); IOUtils.copy(file.getInputStream(j), aos); aos.closeArchiveEntry(); }//from www . ja v a2 s . co m } }
From source file:com.github.wolfposd.jdpkg.deb.DpkgDeb.java
public static void writeDebFile(File destination, File[] inputsfiles) throws ArchiveException, IOException { ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.AR, new FileOutputStream(destination)); for (File file : inputsfiles) { ArArchiveEntry entry = new ArArchiveEntry(file, file.getName()); archive.putArchiveEntry(entry);/*from ww w.j a v a2 s. c om*/ BufferedInputStream input = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(input, archive); input.close(); archive.closeArchiveEntry(); } archive.finish(); archive.close(); }
From source file:deployer.TestUtils.java
public static ByteBuffer createSampleOpenShiftWebAppTarBall() throws IOException, ArchiveException { ByteArrayInputStream bis = new ByteArrayInputStream(createSampleAppTarBall(ArtifactType.WebApp).array()); CompressorInputStream cis = new GzipCompressorInputStream(bis); ArchiveInputStream ais = new TarArchiveInputStream(cis); ByteArrayOutputStream bos = new ByteArrayOutputStream(bis.available() + 2048); CompressorOutputStream cos = new GzipCompressorOutputStream(bos); ArchiveOutputStream aos = new TarArchiveOutputStream(cos); ArchiveEntry nextEntry;// w w w. ja v a 2s .c om while ((nextEntry = ais.getNextEntry()) != null) { aos.putArchiveEntry(nextEntry); IOUtils.copy(ais, aos); aos.closeArchiveEntry(); } ais.close(); cis.close(); bis.close(); TarArchiveEntry entry = new TarArchiveEntry( Paths.get(".openshift", CONFIG_DIRECTORY, "/standalone.xml").toFile()); byte[] xmlData = SAMPLE_STANDALONE_DATA.getBytes(); entry.setSize(xmlData.length); aos.putArchiveEntry(entry); IOUtils.write(xmlData, aos); aos.closeArchiveEntry(); aos.finish(); cos.close(); bos.flush(); return ByteBuffer.wrap(bos.toByteArray()); }
From source file:deployer.TestUtils.java
public static ByteBuffer createSampleOpenShiftWebAppTarBallWithEmptyFiles(String[] filepaths) throws IOException, ArchiveException { ByteArrayInputStream bis = new ByteArrayInputStream(createSampleAppTarBall(ArtifactType.WebApp).array()); CompressorInputStream cis = new GzipCompressorInputStream(bis); ArchiveInputStream ais = new TarArchiveInputStream(cis); ByteArrayOutputStream bos = new ByteArrayOutputStream(bis.available() + 2048); CompressorOutputStream cos = new GzipCompressorOutputStream(bos); ArchiveOutputStream aos = new TarArchiveOutputStream(cos); ArchiveEntry nextEntry;/*from w w w.j a v a 2 s . c o m*/ while ((nextEntry = ais.getNextEntry()) != null) { aos.putArchiveEntry(nextEntry); IOUtils.copy(ais, aos); aos.closeArchiveEntry(); } ais.close(); cis.close(); bis.close(); TarArchiveEntry entry = new TarArchiveEntry( Paths.get(".openshift", CONFIG_DIRECTORY, "/standalone.xml").toFile()); byte[] xmlData = SAMPLE_STANDALONE_DATA.getBytes(); entry.setSize(xmlData.length); aos.putArchiveEntry(entry); IOUtils.write(xmlData, aos); for (int i = 0; i < filepaths.length; i++) { String filepath = filepaths[i]; TarArchiveEntry emptyEntry = new TarArchiveEntry(Paths.get(filepath).toFile()); byte[] emptyData = "".getBytes(); emptyEntry.setSize(emptyData.length); aos.putArchiveEntry(emptyEntry); IOUtils.write(emptyData, aos); aos.closeArchiveEntry(); } aos.finish(); cos.close(); bos.flush(); return ByteBuffer.wrap(bos.toByteArray()); }
From source file:com.github.wolfposd.jdpkg.deb.DpkgDeb.java
public static void fileEntryToDestination(File source, ArchiveOutputStream archive, boolean atRoot) throws IOException { TarArchiveEntry entry;/* ww w . j a v a 2s . co m*/ if (atRoot) { entry = new TarArchiveEntry(source.getName()); } else { entry = new TarArchiveEntry(source.getPath().replace(BuildFile, "")); } entry.setSize(source.length()); entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE); archive.putArchiveEntry(entry); BufferedInputStream input = new BufferedInputStream(new FileInputStream(source)); IOUtils.copy(input, archive); input.close(); archive.closeArchiveEntry(); }
From source file:deployer.TestUtils.java
public static ByteBuffer createSampleAppTarBall(ArtifactType type) throws IOException, ArchiveException { ByteArrayOutputStream bos = new ByteArrayOutputStream(8096); CompressorOutputStream gzs = new GzipCompressorOutputStream(bos); ArchiveOutputStream aos = new TarArchiveOutputStream(gzs); {//from w w w . j a va 2s . c o m TarArchiveEntry nextEntry = new TarArchiveEntry(CONFIG_DIRECTORY + "/app.conf"); byte[] sampleConf = SAMPLE_CONF_DATA.getBytes(); nextEntry.setSize(sampleConf.length); aos.putArchiveEntry(nextEntry); IOUtils.write(sampleConf, aos); aos.closeArchiveEntry(); } if (type != ArtifactType.WebApp) { TarArchiveEntry nextEntry = new TarArchiveEntry("bin/myApplication.jar"); byte[] jarData = SAMPLE_JAR_DATA.getBytes(); nextEntry.setSize(jarData.length); aos.putArchiveEntry(nextEntry); IOUtils.write(jarData, aos); aos.closeArchiveEntry(); } else { TarArchiveEntry nextEntry = new TarArchiveEntry("deployments/ROOT.war"); byte[] jarData = SAMPLE_JAR_DATA.getBytes(); nextEntry.setSize(jarData.length); aos.putArchiveEntry(nextEntry); IOUtils.write(jarData, aos); aos.closeArchiveEntry(); } aos.finish(); gzs.close(); bos.flush(); return ByteBuffer.wrap(bos.toByteArray()); }