Example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry ZipArchiveEntry

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry ZipArchiveEntry

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry ZipArchiveEntry.

Prototype

public ZipArchiveEntry(ZipArchiveEntry entry) throws ZipException 

Source Link

Document

Creates a new zip entry with fields taken from the specified zip entry.

Usage

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();//w w w .  ja  v  a2s  .c o m

    os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
    IOUtils.copy(new FileInputStream(file2), os);
    os.closeArchiveEntry();
    out.flush();
    os.close();
}

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;/*from   ww  w .  j  ava2s .c  om*/

    try {
        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. j  ava 2s .  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:com.openkm.util.ArchiveUtils.java

/**
 * Create ZIP archive from file//w  ww.j a v a 2s.  c o m
 */
public static void createZip(File path, OutputStream os) throws IOException {
    log.debug("createZip({}, {})", new Object[] { path, os });

    if (path.exists() && path.canRead()) {
        ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os);
        zaos.setComment("Generated by OpenKM");
        zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
        zaos.setUseLanguageEncodingFlag(true);
        zaos.setFallbackToUTF8(true);
        zaos.setEncoding("UTF-8");

        log.debug("FILE {}", path);
        ZipArchiveEntry zae = new ZipArchiveEntry(path.getName());
        zaos.putArchiveEntry(zae);
        FileInputStream fis = new FileInputStream(path);
        IOUtils.copy(fis, zaos);
        fis.close();
        zaos.closeArchiveEntry();

        zaos.flush();
        zaos.finish();
        zaos.close();
    } else {
        throw new IOException("Can't access " + path);
    }

    log.debug("createZip: void");
}

From source file:com.ikon.util.ArchiveUtils.java

/**
 * Recursively create ZIP archive from directory 
 *//*from  w w  w. ja v a  2  s.co m*/
public static void createZip(File path, String root, OutputStream os) throws IOException {
    log.debug("createZip({}, {}, {})", new Object[] { path, root, os });

    if (path.exists() && path.canRead()) {
        ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os);
        zaos.setComment("Generated by openkm");
        zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
        zaos.setUseLanguageEncodingFlag(true);
        zaos.setFallbackToUTF8(true);
        zaos.setEncoding("UTF-8");

        // Prevents java.util.zip.ZipException: ZIP file must have at least one entry
        ZipArchiveEntry zae = new ZipArchiveEntry(root + "/");
        zaos.putArchiveEntry(zae);
        zaos.closeArchiveEntry();

        createZipHelper(path, zaos, root);

        zaos.flush();
        zaos.finish();
        zaos.close();
    } else {
        throw new IOException("Can't access " + path);
    }

    log.debug("createZip: void");
}

From source file:fr.duminy.jbackup.core.archive.zip.ZipArchiveOutputStream.java

@Override
public void addEntry(String name, InputStream input) throws IOException {
    output.putArchiveEntry(new ZipArchiveEntry(name));
    IOUtils.copy(input, output);//from  w  w  w  .  j  a  va 2 s.  c o  m
    output.closeArchiveEntry();
}

From source file:edu.jhu.hlt.acute.archivers.zip.ZipArchiver.java

@Override
public void addEntry(Archivable arch) throws IOException {
    final String fn = arch.getFileName();
    ZipArchiveEntry entry = new ZipArchiveEntry(fn);
    byte[] cbytes = arch.getBytes();
    entry.setSize(cbytes.length);/*w ww . j a v a 2 s.co m*/
    this.zout.putArchiveEntry(entry);
    try (ByteArrayInputStream bis = new ByteArrayInputStream(cbytes)) {
        IOUtils.copy(bis, zout);
        this.zout.closeArchiveEntry();
    }
}

From source file:br.com.thiaguten.archive.ZipArchive.java

@Override
protected ArchiveEntry createArchiveEntry(String path, long size, byte[] content) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(path);
    zipEntry.setSize(size);// ww  w  .j  ava 2  s  .  com
    return zipEntry;
}

From source file:hudson.util.io.ZipArchiver.java

@Override
public void visitSymlink(final File f, final String target, final String relativePath) throws IOException {
    int mode = IOUtils.lmode(f);
    ZipArchiveEntry zae = new ZipArchiveEntry(relativePath);
    if (mode != -1) {
        zae.setUnixMode(mode);/*  www.  jav a 2 s. c o  m*/
    }
    zae.setTime(f.lastModified());
    zip.putArchiveEntry(zae);
    zip.write(target.getBytes(StandardCharsets.UTF_8), 0, target.length());
    zip.closeArchiveEntry();
    entriesWritten++;
}

From source file:com.impetus.ankush.agent.utils.ZipFiles.java

/**
 * Zip file./*from  w  w w.  j  av a2  s. c om*/
 *
 * @param filePath the file path
 * @return the string
 */
public String zipFile(String filePath) {
    try {
        /* Create Output Stream that will have final zip files */
        OutputStream zipOutput = new FileOutputStream(new File(filePath + ".zip"));
        /*
         * Create Archive Output Stream that attaches File Output Stream / and
         * specifies type of compression
         */
        ArchiveOutputStream logicalZip = new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipOutput);
        /* Create Archieve entry - write header information */
        logicalZip.putArchiveEntry(new ZipArchiveEntry(FilenameUtils.getName(filePath)));
        /* Copy input file */
        IOUtils.copy(new FileInputStream(new File(filePath)), logicalZip);
        /* Close Archieve entry, write trailer information */
        logicalZip.closeArchiveEntry();

        /* Finish addition of entries to the file */
        logicalZip.finish();
        /* Close output stream, our files are zipped */
        zipOutput.close();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        return null;
    }
    return filePath + ".zip";
}