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: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
 */// w w  w . ja v  a2  s .com
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.opengion.fukurou.util.ZipArchive.java

/**
 * ZIP????//from  w  w  w. j a  v  a  2  s  . c o  m
 * ???File?????????
 * ???????????????
 * ??????
 *
 * @param list   ZIP???
 * @param zos    ZIPOutputStream
 * @param prefix ?
 * @param files  ??
 * @throws IOException ????
 * @og.rev 4.1.0.2 (2008/02/01) ?
 * @og.rev 5.1.9.0 (2010/08/01) ? ?BufferedInputStream ??????
 */
private static void addZipEntry(final List<File> list, final ZipArchiveOutputStream zos, final String prefix,
        final File[] files) {
    File tmpFile = null;
    try {
        for (File fi : files) {
            tmpFile = fi; // ?
            list.add(fi);
            if (fi.isDirectory()) {
                String entryName = prefix + fi.getName() + "/";
                ZipArchiveEntry zae = new ZipArchiveEntry(entryName);
                zos.putArchiveEntry(zae);
                zos.closeArchiveEntry();

                addZipEntry(list, zos, entryName, fi.listFiles());
            } else {
                String entryName = prefix + fi.getName();
                ZipArchiveEntry zae = new ZipArchiveEntry(entryName);
                zos.putArchiveEntry(zae);
                InputStream is = new BufferedInputStream(new FileInputStream(fi));
                IOUtils.copy(is, zos);
                zos.closeArchiveEntry();
                Closer.ioClose(is);
            }
        }
    } catch (FileNotFoundException ex) {
        String errMsg = "??????[??=" + tmpFile
                + "]";
        throw new RuntimeException(errMsg, ex);
    } catch (IOException ex) {
        String errMsg = "ZIP?????[??=" + tmpFile + "]";
        throw new RuntimeException(errMsg, ex);
    }
}

From source file:org.openo.commontosca.catalog.mdserver.csar.export.ZipExporter.java

private void addManifest() throws IOException {
    zos.putArchiveEntry(new ZipArchiveEntry(Constants.TOSCA_META));
    PrintWriter pw = new PrintWriter(zos);
    pw.println("TOSCA-Meta-Version: 1.0");
    pw.println("CSAR-Version: 1.0");
    String versionString = "Created-By: Winery " + "zte";
    pw.println(versionString);//from  w w w.  ja  va2s  . co m
    pw.println("Entry-Definitions: " + "Definitions/" + serviceTemplateId.getId() + "."
            + MimeTypes.YAML.getValue());
    pw.println();
    // add definition
    pw.println("Name: " + "Definitions/" + serviceTemplateId.getId() + "." + MimeTypes.YAML.getValue());
    pw.println("Content-Type: " + HttpMimeTypes.MIMETYPE_TOSCA_DEFINITIONS);
    pw.println();
    pw.flush();
    pw.close();
    zos.closeArchiveEntry();
}

From source file:org.openo.commontosca.catalog.mdserver.csar.export.ZipExporter.java

private void addCsarMeta(CsarMetaEntity csarInfo) throws IOException {
    zos.putArchiveEntry(new ZipArchiveEntry(Constants.CSAR_META));
    StringBuffer buffer = new StringBuffer();
    buffer.append("Type:");
    buffer.append(csarInfo.getType());//from  ww w.  j  a v  a 2  s . c om
    buffer.append("\n");

    buffer.append("Version:");
    buffer.append(csarInfo.getVersion());
    buffer.append("\n");

    buffer.append("Provider:");
    buffer.append(csarInfo.getProvider());

    PrintWriter pw = new PrintWriter(zos);
    pw.print(buffer.toString());
    pw.flush();
    pw.close();
    zos.closeArchiveEntry();
}

From source file:org.openo.commontosca.catalog.mdserver.csar.export.ZipExporter.java

/**
 * add File to zip/*from  w ww.  j  a  va2s. c o  m*/
 * 
 * @param inputStream input file stream
 * @param dest file
 * @throws MdCommonException
 */
private void addFileToZipArchive(InputStream inputStream, String file) throws MdCommonException {
    try {
        zos.putArchiveEntry(new ZipArchiveEntry(file));
        if (inputStream != null)
            IOUtils.copy(inputStream, zos);
        zos.closeArchiveEntry();
    } catch (IOException e) {
        throwMdCommonException("add file {} to zipArchive faild.file:" + file + "errorMsg:" + e.getMessage());
    } finally {
        if (inputStream != null)
            closeInputStream(inputStream);
    }
}

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

public static void protectVCF(File targetFile, File vCardFile, char[] password) throws Exception {
    ZipArchiveOutputStream out = null;//  ww  w .j  a  v a 2 s.com
    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   w  w  w  .  ja  v  a  2 s . com
 * 
 * @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.pepstock.jem.commands.CreateNode.java

private static final void zip(File directory, File base, ZipArchiveOutputStream zos) throws IOException {
    File[] files = directory.listFiles();
    for (int i = 0, n = files.length; i < n; i++) {
        if (files[i].isDirectory()) {
            zip(files[i], base, zos);//ww w  . j  ava2 s  .com
        } else {
            FileInputStream in = null;
            try {
                in = new FileInputStream(files[i]);
                ZipArchiveEntry entry = new ZipArchiveEntry(
                        files[i].getPath().substring(base.getPath().length() + 1));
                zos.putArchiveEntry(entry);
                IOUtils.copy(in, zos);
                zos.closeArchiveEntry();
            } catch (IOException e) {
                throw e;
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        }
    }
}

From source file:org.sead.nds.repository.BagGenerator.java

private void createDir(final String name) throws IOException, ExecutionException, InterruptedException {

    ZipArchiveEntry archiveEntry = new ZipArchiveEntry(name);
    archiveEntry.setMethod(ZipEntry.DEFLATED);
    InputStreamSupplier supp = new InputStreamSupplier() {
        public InputStream get() {
            return new ByteArrayInputStream(("").getBytes());
        }//  w  w  w.jav a  2s  .co m
    };

    addEntry(archiveEntry, supp);
}

From source file:org.sead.nds.repository.BagGenerator.java

private void createFileFromString(final String name, final String content)
        throws IOException, ExecutionException, InterruptedException {

    ZipArchiveEntry archiveEntry = new ZipArchiveEntry(name);
    archiveEntry.setMethod(ZipEntry.DEFLATED);
    InputStreamSupplier supp = new InputStreamSupplier() {
        public InputStream get() {
            try {
                return new ByteArrayInputStream(content.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();// w  ww. j a v  a 2  s. co  m
            }
            return null;
        }
    };

    addEntry(archiveEntry, supp);
}