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:net.duckling.ddl.service.export.impl.ExportAttachSaver.java

public void save(String filename, InputStream in) {
    String newFilename = getNormalFilename(filename);
    int dotIndex = newFilename.lastIndexOf(".");
    dotIndex = (dotIndex <= 0) ? newFilename.length() : dotIndex;
    newFilename = newFilename.substring(0, dotIndex) + "_" + rid + "_" + tid + "_" + LynxConstants.TYPE_FILE
            + newFilename.substring(dotIndex, newFilename.length());
    try {/*from w w  w.  ja  va  2s.  co m*/
        newFilename = java.net.URLDecoder.decode(newFilename, "utf8");
        out.putArchiveEntry(new ZipArchiveEntry(path + "/" + newFilename));
        IOUtils.copy(in, out);
        in.close();
        out.closeArchiveEntry();
    } catch (UnsupportedEncodingException e) {
        LOGGER.error("????", e);
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }

}

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

/**
 * Recursively create ZIP archive from directory helper utility 
 *//*from  w ww.  j a v a  2 s .c o m*/
private static void createZipHelper(File fs, ZipArchiveOutputStream zaos, String zePath) throws IOException {
    log.debug("createZipHelper({}, {}, {})", new Object[] { fs, zaos, zePath });
    File[] files = fs.listFiles();

    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            log.debug("DIRECTORY {}", files[i]);
            ZipArchiveEntry zae = new ZipArchiveEntry(zePath + "/" + files[i].getName() + "/");
            zaos.putArchiveEntry(zae);
            zaos.closeArchiveEntry();

            createZipHelper(files[i], zaos, zePath + "/" + files[i].getName());
        } else {
            log.debug("FILE {}", files[i]);
            ZipArchiveEntry zae = new ZipArchiveEntry(zePath + "/" + files[i].getName());
            zaos.putArchiveEntry(zae);
            FileInputStream fis = new FileInputStream(files[i]);
            IOUtils.copy(fis, zaos);
            fis.close();
            zaos.closeArchiveEntry();
        }
    }

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

From source file:big.zip.java

/**
 * /*  www .j a va2  s  .c  om*/
 * @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.openkm.util.ArchiveUtils.java

/**
 * Recursively create ZIP archive from directory
 *///from  w w  w.ja v a  2 s.  c o 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:com.silverpeas.util.ZipManager.java

/**
 * Compress a file into a zip file./* w  w  w  .  ja  va 2 s .  c o m*/
 *
 * @param filePath
 * @param zipFilePath
 * @return
 * @throws IOException
 */
public static long compressFile(String filePath, String zipFilePath) throws IOException {
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new FileOutputStream(zipFilePath));
    InputStream in = new FileInputStream(filePath);
    try {
        // cration du flux zip
        zos = new ZipArchiveOutputStream(new FileOutputStream(zipFilePath));
        zos.setFallbackToUTF8(true);
        zos.setCreateUnicodeExtraFields(NOT_ENCODEABLE);
        zos.setEncoding(CharEncoding.UTF_8);
        String entryName = FilenameUtils.getName(filePath);
        entryName = entryName.replace(File.separatorChar, '/');
        zos.putArchiveEntry(new ZipArchiveEntry(entryName));
        IOUtils.copy(in, zos);
        zos.closeArchiveEntry();
        return new File(zipFilePath).length();
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(zos);
    }
}

From source file:com.haulmont.cuba.core.sys.logging.LogArchiver.java

private static ArchiveEntry newTailArchive(String name, byte[] tail) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(name);
    zipEntry.setSize(tail.length);/*w ww  .j  av a2  s . c o  m*/
    zipEntry.setCompressedSize(zipEntry.getSize());
    CRC32 crc32 = new CRC32();
    crc32.update(tail);
    zipEntry.setCrc(crc32.getValue());
    return zipEntry;
}

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();/*from w ww .  ja va 2  s.c om*/
    aos.close();

    // 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.google.jenkins.plugins.persistentmaster.volume.zip.ZipCreator.java

private void copySymlink(Path file, String filenameInZip) throws IOException {
    logger.finer("Adding symlink: " + file + " with filename: " + filenameInZip);
    Path symlinkTarget = Files.readSymbolicLink(file);
    // Unfortunately, there is no API method to create a symlink in a ZIP file,
    // however, a symlink entry can easily be created by hand.
    // The requirements for a symlink entry are:
    //  - the unix mode must have the LINK_FLAG set
    //  - the content must contain the target of the symlink as UTF8 string
    ZipArchiveEntry entry = new ZipArchiveEntry(filenameInZip);
    entry.setUnixMode(entry.getUnixMode() | UnixStat.LINK_FLAG);
    zipStream.putArchiveEntry(entry);/*from   w  w w.j ava 2s  .c  om*/
    zipStream.write(symlinkTarget.toString().getBytes(StandardCharsets.UTF_8));
    zipStream.closeArchiveEntry();
}

From source file:algorithm.ZipPackaging.java

private void archiveFile(ArchiveOutputStream archiveOutputStream, File file)
        throws IOException, FileNotFoundException {
    archiveOutputStream.putArchiveEntry(new ZipArchiveEntry(file.getName()));
    FileInputStream inputStream = new FileInputStream(file);
    IOUtils.copy(inputStream, archiveOutputStream);
    archiveOutputStream.closeArchiveEntry();
    inputStream.close();//from  w  ww  .  j a v  a  2s.  c  o m
}

From source file:com.haulmont.cuba.core.sys.logging.LogArchiver.java

private static ArchiveEntry newArchive(File file) throws IOException {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(file.getName());
    zipEntry.setSize(file.length());/*from  w w w.ja  v a2s  .  c  o m*/
    zipEntry.setCompressedSize(zipEntry.getSize());
    zipEntry.setCrc(FileUtils.checksumCRC32(file));
    return zipEntry;
}