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

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

Introduction

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

Prototype

public void setSize(long size) 

Source Link

Document

Sets the uncompressed size of the entry data.

Usage

From source file:org.envirocar.app.util.Util.java

/**
 * Android devices up to version 2.3.7 have a bug in the native
 * Zip archive creation, making the archive unreadable with some
 * programs.//from   w w w.  j  a  v  a 2s.  c o  m
 * 
 * @param files
 *            the list of files of the target archive
 * @param target
 *            the target filename
 * @throws IOException
 */
public static void zipInteroperable(List<File> files, String target) throws IOException {
    ZipArchiveOutputStream aos = null;
    OutputStream out = null;
    try {
        File tarFile = new File(target);
        out = new FileOutputStream(tarFile);

        try {
            aos = (ZipArchiveOutputStream) new ArchiveStreamFactory()
                    .createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);
        } catch (ArchiveException e) {
            throw new IOException(e);
        }

        for (File file : files) {
            ZipArchiveEntry entry = new ZipArchiveEntry(file, file.getName());
            entry.setSize(file.length());
            aos.putArchiveEntry(entry);
            IOUtils.copy(new FileInputStream(file), aos);
            aos.closeArchiveEntry();
        }

    } catch (IOException e) {
        throw e;
    } finally {
        aos.finish();
        out.close();
    }
}

From source file:org.jwebsocket.util.Tools.java

/**
 * Compress a byte array using zip compression
 *
 * @param aBA/*ww w  .ja  va  2  s . c  o m*/
 * @param aBase64Encode if TRUE, the result is Base64 encoded
 * @return
 * @throws Exception
 */
public static byte[] zip(byte[] aBA, Boolean aBase64Encode) throws Exception {
    ByteArrayOutputStream lBAOS = new ByteArrayOutputStream();
    ArchiveOutputStream lAOS = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
            lBAOS);
    ZipArchiveEntry lZipEntry = new ZipArchiveEntry("temp.zip");
    lZipEntry.setSize(aBA.length);
    lAOS.putArchiveEntry(lZipEntry);
    lAOS.write(aBA);
    lAOS.closeArchiveEntry();
    lAOS.flush();
    lAOS.close();

    if (aBase64Encode) {
        aBA = Base64.encodeBase64(lBAOS.toByteArray());
    } else {
        aBA = lBAOS.toByteArray();
    }

    return aBA;
}

From source file:org.mycore.services.zipper.MCRZipServlet.java

@Override
protected void sendCompressedFile(MCRPath file, BasicFileAttributes attrs, ZipArchiveOutputStream container)
        throws IOException {
    ZipArchiveEntry entry = new ZipArchiveEntry(getFilename(file));
    entry.setTime(attrs.lastModifiedTime().toMillis());
    entry.setSize(attrs.size());
    container.putArchiveEntry(entry);/* ww w . j  a va  2  s.c  om*/
    try {
        Files.copy(file, container);
    } finally {
        container.closeArchiveEntry();
    }
}

From source file:org.mycore.services.zipper.MCRZipServlet.java

@Override
protected void sendMetadataCompressed(String fileName, byte[] content, long lastModified,
        ZipArchiveOutputStream container) throws IOException {
    ZipArchiveEntry entry = new ZipArchiveEntry(fileName);
    entry.setSize(content.length);
    entry.setTime(lastModified);/* w  w w . ja  v  a 2 s .  com*/
    container.putArchiveEntry(entry);
    container.write(content);
    container.closeArchiveEntry();
}

From source file:org.onehippo.forge.content.exim.repository.jaxrs.util.ZipCompressUtils.java

/**
 * Add a ZIP entry to {@code zipOutput} with the given {@code entryName} and {@code bytes} starting from
 * {@code offset} in {@code length}.//www.ja  v a  2  s.  c  om
 * @param entryName ZIP entry name
 * @param bytes the byte array to fill in for the ZIP entry
 * @param offset the starting offset index to read from the byte array
 * @param length the length to read from the byte array
 * @param zipOutput ZipArchiveOutputStream instance
 * @throws IOException if IO exception occurs
 */
public static void addEntryToZip(String entryName, byte[] bytes, int offset, int length,
        ZipArchiveOutputStream zipOutput) throws IOException {
    ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
    entry.setSize(length);

    try {
        zipOutput.putArchiveEntry(entry);
        zipOutput.write(bytes, offset, length);
    } finally {
        zipOutput.closeArchiveEntry();
    }
}

From source file:org.onehippo.forge.content.exim.repository.jaxrs.util.ZipCompressUtils.java

/**
 * Add ZIP entries to {@code zipOutput} by selecting all the descendant files under the {@code baseFolder},
 * starting with the ZIP entry name {@code prefix}.
 * @param baseFolder base folder to find child files underneath
 * @param prefix the prefix of ZIP entry name
 * @param zipOutput ZipArchiveOutputStream instance
 * @throws IOException if IO exception occurs
 *///from   w w  w.j a va 2s  .co m
public static void addFileEntriesInFolderToZip(File baseFolder, String prefix, ZipArchiveOutputStream zipOutput)
        throws IOException {
    for (File file : baseFolder.listFiles()) {
        String entryName = (StringUtils.isEmpty(prefix)) ? file.getName() : (prefix + "/" + file.getName());

        if (file.isFile()) {
            ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
            entry.setSize(file.length());
            InputStream input = null;

            try {
                zipOutput.putArchiveEntry(entry);
                input = new FileInputStream(file);
                IOUtils.copyLarge(input, zipOutput);
            } finally {
                IOUtils.closeQuietly(input);
                zipOutput.closeArchiveEntry();
            }
        } else {
            addFileEntriesInFolderToZip(file, entryName, zipOutput);
        }
    }
}

From source file:org.panbox.core.identitymgmt.VCardProtector.java

public static void protectVCF(File targetFile, File vCardFile, char[] password) throws Exception {
    ZipArchiveOutputStream out = null;/*  w w w . j  a  v a  2  s.c om*/
    try {
        out = new ZipArchiveOutputStream(new FileOutputStream(targetFile));

        byte[] vCardData = IOUtils.toByteArray(new FileInputStream(vCardFile));
        byte[] passwordBytes = Utils.toBytes(password);

        vcfMac.init(new SecretKeySpec(passwordBytes, KeyConstants.VCARD_HMAC));
        byte[] hmac = vcfMac.doFinal(vCardData);

        String fileName = Utils.bytesToHex(hmac);

        // first entry is the vcard itself
        ZipArchiveEntry entry = new ZipArchiveEntry(vCardFile.getName());
        entry.setSize(vCardData.length);
        out.putArchiveEntry(entry);
        out.write(vCardData);
        out.flush();
        out.closeArchiveEntry();

        // second entry is the hmac value
        entry = new ZipArchiveEntry(fileName);
        entry.setSize(fileName.length());
        out.putArchiveEntry(entry);
        out.closeArchiveEntry();
        out.flush();
    } catch (IOException | InvalidKeyException e) {
        logger.error("Could not create protected VCF export file!", e);
        throw e;
    } finally {
        if (out != null) {
            out.flush();
            out.close();
        }
    }
}

From source file:org.panbox.core.pairing.file.PanboxFilePairingUtils.java

/**
 * Stores a pairing file at the specified path for the specified device and
 * type/*from ww  w.j  ava2 s . c  o m*/
 * 
 * @param outputFile
 *            Pairing file to be saved
 * @param devicename
 *            Name of the device that should be paired
 * @param password
 *            Password of the identity
 */
public static PanboxFilePairingWriteReturnContainer storePairingFile(File outputFile, String devicename,
        char[] password, PairingType type, DeviceType devType, String eMail, String firstName, String lastName,
        PrivateKey privEncKey, X509Certificate encCert, PrivateKey privSignKey, X509Certificate signCert,
        Map<String, X509Certificate> devices, Collection<VCard> contacts)
        throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
    logger.debug("PanboxFilePairingUtils : storePairingFile : Storing pairing container to: "
            + outputFile.getAbsolutePath());

    ZipArchiveOutputStream out = new ZipArchiveOutputStream(new FileOutputStream(outputFile));

    // 1. add device name to pairing file
    ZipArchiveEntry entry = new ZipArchiveEntry("devicename");
    entry.setSize(devicename.getBytes().length);
    out.putArchiveEntry(entry);

    out.write(devicename.getBytes());
    out.flush();

    out.closeArchiveEntry();

    // 2. add device name to pairing file
    entry = new ZipArchiveEntry("email");
    entry.setSize(eMail.getBytes().length);
    out.putArchiveEntry(entry);

    out.write(eMail.getBytes());
    out.flush();

    out.closeArchiveEntry();

    // 3. add device name to pairing file
    entry = new ZipArchiveEntry("firstname");
    entry.setSize(firstName.getBytes().length);
    out.putArchiveEntry(entry);

    out.write(firstName.getBytes());
    out.flush();

    out.closeArchiveEntry();

    // 4. add device name to pairing file
    entry = new ZipArchiveEntry("lastname");
    entry.setSize(lastName.getBytes().length);
    out.putArchiveEntry(entry);

    out.write(lastName.getBytes());
    out.flush();

    out.closeArchiveEntry();

    // 5. generate and add a new device key + cert for the newly device
    KeyPair devKey = CryptCore.generateKeypair();
    X509Certificate devCert = CryptCore.createSelfSignedX509Certificate(devKey.getPrivate(), devKey.getPublic(),
            new PairingIPersonDummy(eMail, firstName, lastName));

    KeyStore devKeyStore = KeyStore.getInstance("PKCS12");
    devKeyStore.load(null, null);
    devKeyStore.setKeyEntry(devicename, (Key) devKey.getPrivate(), password, new Certificate[] { devCert });
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    devKeyStore.store(baos, password);
    baos.flush();

    byte[] data = baos.toByteArray();
    entry = new ZipArchiveEntry("devicekey.p12");
    entry.setSize(data.length);
    out.putArchiveEntry(entry);
    out.write(data);
    out.flush();

    out.closeArchiveEntry();

    // 6. add device certs and names for all known devices

    baos = new ByteArrayOutputStream();
    ByteArrayOutputStream deviceNamesFile = new ByteArrayOutputStream();
    KeyStore deviceKeyStore = KeyStore.getInstance("BKS");
    deviceKeyStore.load(null, null);
    int i = 0;

    for (Entry<String, X509Certificate> device : devices.entrySet()) {
        deviceKeyStore.setCertificateEntry("device" + i, device.getValue());
        deviceNamesFile.write(("device" + i + DELIMITER + device.getKey() + "\n").getBytes());
        ++i;
    }

    deviceKeyStore.store(baos, password);
    baos.flush();
    deviceNamesFile.flush();

    byte[] data2 = deviceNamesFile.toByteArray();
    entry = new ZipArchiveEntry("knownDevices.list");
    entry.setSize(data2.length);
    out.putArchiveEntry(entry);
    out.write(data2);
    out.flush();

    data = baos.toByteArray();
    entry = new ZipArchiveEntry("knownDevices.bks");
    entry.setSize(data.length);
    out.putArchiveEntry(entry);
    out.write(data);
    out.flush();

    // 7. add vcard for all known contacts

    File tempContacts = File.createTempFile("panboxContacts", null);
    AbstractAddressbookManager.exportContacts(contacts, tempContacts);
    FileInputStream fis = new FileInputStream(tempContacts);
    data = new byte[(int) tempContacts.length()];
    fis.read(data);
    fis.close();
    tempContacts.delete();

    entry = new ZipArchiveEntry("contacts.vcard");
    entry.setSize(data.length);
    out.putArchiveEntry(entry);
    out.write(data);
    out.flush();

    // 8. add owner certs or keys in case of main/restricted
    KeyStore ownerKeyStore = null;
    if (type == PairingType.MASTER) {
        ownerKeyStore = KeyStore.getInstance("PKCS12");
        ownerKeyStore.load(null, null);
        ownerKeyStore.setKeyEntry("ownerEncKey", privEncKey, password, new Certificate[] { encCert });
        ownerKeyStore.setKeyEntry("ownerSignKey", privSignKey, password, new Certificate[] { signCert });
        entry = new ZipArchiveEntry("ownerKeys.p12");
    } else {
        ownerKeyStore = KeyStore.getInstance("BKS");
        ownerKeyStore.load(null, null);
        ownerKeyStore.setCertificateEntry("ownerEncCert", encCert);
        ownerKeyStore.setCertificateEntry("ownerSignCert", signCert);
        entry = new ZipArchiveEntry("ownerCerts.bks");
    }
    baos = new ByteArrayOutputStream();
    ownerKeyStore.store(baos, password);
    baos.flush();

    data = baos.toByteArray();
    entry.setSize(data.length);
    out.putArchiveEntry(entry);
    out.write(data);
    out.flush();

    out.closeArchiveEntry();

    out.flush();
    out.close();
    logger.debug("PanboxFilePairingUtils : storePairingFile : Storing pairing container finished.");

    return new PanboxFilePairingWriteReturnContainer(devicename, devCert, devType);
}

From source file:org.trustedanalytics.servicebroker.gearpump.service.file.FileHelper.java

public static byte[] prepareZipFile(byte[] zipFileTestContent) throws IOException {
    ByteArrayOutputStream byteOutput = null;
    ZipArchiveOutputStream zipOutput = null;
    try {/*ww  w .j  av  a 2s. c  o  m*/
        byteOutput = new ByteArrayOutputStream();
        zipOutput = new ZipArchiveOutputStream(byteOutput);
        ZipArchiveEntry entry = new ZipArchiveEntry(FILE_NAME);
        entry.setSize(zipFileTestContent.length);
        addArchiveEntry(zipOutput, entry, zipFileTestContent);
    } finally {
        zipOutput.close();
        byteOutput.close();
    }

    return byteOutput.toByteArray();
}

From source file:org.waarp.common.tar.ZipUtility.java

/**
 * Recursive traversal to add files/*from   w  w w.  j a  va  2 s  . c  o m*/
 * 
 * @param root
 * @param file
 * @param zaos
 * @param absolute
 * @throws IOException
 */
private static void recurseFiles(File root, File file, ZipArchiveOutputStream zaos, boolean absolute)
        throws IOException {
    if (file.isDirectory()) {
        // recursive call
        File[] files = file.listFiles();
        for (File file2 : files) {
            recurseFiles(root, file2, zaos, absolute);
        }
    } else if ((!file.getName().endsWith(".zip")) && (!file.getName().endsWith(".ZIP"))) {
        String filename = null;
        if (absolute) {
            filename = file.getAbsolutePath().substring(root.getAbsolutePath().length());
        } else {
            filename = file.getName();
        }
        ZipArchiveEntry zae = new ZipArchiveEntry(filename);
        zae.setSize(file.length());
        zaos.putArchiveEntry(zae);
        FileInputStream fis = new FileInputStream(file);
        IOUtils.copy(fis, zaos);
        zaos.closeArchiveEntry();
    }
}