Example usage for java.util.zip ZipEntry ZipEntry

List of usage examples for java.util.zip ZipEntry ZipEntry

Introduction

In this page you can find the example usage for java.util.zip ZipEntry ZipEntry.

Prototype

public ZipEntry(ZipEntry e) 

Source Link

Document

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

Usage

From source file:it.geosolutions.mariss.wps.ppio.OutputResourcesPPIO.java

private static void addToZip(File f, ZipOutputStream zos) {

    FileInputStream in = null;/*  ww w.  j ava2s  .c o m*/
    try {
        in = new FileInputStream(f);
        zos.putNextEntry(new ZipEntry(f.getName()));
        IOUtils.copy(in, zos);
        zos.closeEntry();
    } catch (IOException e) {
        LOGGER.severe(e.getMessage());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                LOGGER.severe(e.getMessage());
            }
        }
    }
}

From source file:de.tudarmstadt.ukp.clarin.webanno.api.dao.ZipUtils.java

private static void addToZip(ZipOutputStream zip, File aBasePath, File aPath) throws IOException {
    if (aPath.isDirectory()) {
        for (File file : aPath.listFiles()) {
            addToZip(zip, aBasePath, file);
        }//from w ww.j  av a 2  s  . c o  m
    } else {
        FileInputStream in = null;
        try {
            in = new FileInputStream(aPath);
            String relativePath = aBasePath.toURI().relativize(aPath.toURI()).getPath();
            zip.putNextEntry(new ZipEntry(relativePath));
            IOUtils.copy(in, zip);
        } finally {
            closeQuietly(in);
        }
    }
}

From source file:jobhunter.persistence.Persistence.java

private Optional<Profile> _readProfile(final File file) {

    try (ZipFile zfile = new ZipFile(file)) {
        l.debug("Reading profile from JHF File");
        final InputStream in = zfile.getInputStream(new ZipEntry("profile.xml"));

        MessageDigest md = MessageDigest.getInstance("MD5");
        DigestInputStream dis = new DigestInputStream(in, md);

        final Object obj = xstream.fromXML(dis);

        updateLastMod(file, md);//ww  w  .  jav  a 2s  .co  m

        return Optional.of((Profile) obj);
    } catch (Exception e) {
        l.error("Failed to read file: {}", e.getMessage());
    }

    return Optional.empty();
}

From source file:ai.serotonin.backup.Backup.java

private static void addZipEntry(final ZipOutputStream zip, final String prefix, final String path)
        throws IOException {
    final ZipEntry e = new ZipEntry(path);
    zip.putNextEntry(e);//from www. j a  v  a2s  .c om

    try (FileInputStream in = new FileInputStream(new File(prefix, path))) {
        final long len = IOUtils.copy(in, zip);
        if (LOG.isDebugEnabled())
            LOG.debug("Wrote " + path + ", " + len + " bytes ");
    }

    zip.closeEntry();
}

From source file:au.com.jwatmuff.eventmanager.util.ZipUtils.java

private static void addFolderToZip(File folder, ZipOutputStream zip, String baseName) throws IOException {
    File[] files = folder.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            addFolderToZip(file, zip, baseName);
        } else {/*from w  w w. j  a  v  a2 s. c o  m*/
            String name = file.getAbsolutePath().substring(baseName.length());
            ZipEntry zipEntry = new ZipEntry(name);
            zip.putNextEntry(zipEntry);
            IOUtils.copy(new FileInputStream(file), zip);
            zip.closeEntry();
        }
    }
}

From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java

private static void zipDirectory(File directory, String name, ZipOutputStream zos) throws IOException {
    // *MUST* append the trailing slash for a ZipEntry to identify an
    // entry as a directory.
    name += "/";/*from  www. j  a v a  2 s  .c o  m*/

    zos.putNextEntry(new ZipEntry(name));
    zos.closeEntry();

    String[] entryList = directory.list();

    for (int i = 0; i < entryList.length; ++i) {
        File f = new File(directory, entryList[i]);

        if (f.isDirectory()) {
            zipDirectory(f, name + f.getName(), zos);
        } else {
            FileInputStream fis = new FileInputStream(f);
            ZipEntry entry = new ZipEntry(name + f.getName());
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesIn = 0;

            zos.putNextEntry(entry);

            while ((bytesIn = fis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesIn);
            }

            fis.close();
            zos.closeEntry();
        }
    }
}

From source file:com.mobeelizer.java.sync.MobeelizerOutputData.java

public void writeFile(final String guid, final InputStream stream) {
    try {/*from   w w w.j  a va 2 s .c o  m*/
        zip.putNextEntry(new ZipEntry(guid));
        copy(stream, zip);
        zip.closeEntry();
    } catch (IOException e) {
        closeQuietly(zip);
        closeQuietly(dataOutputStream);
        throw new IllegalStateException(e.getMessage(), e);
    }
}

From source file:com.liferay.ide.server.remote.AbstractRemoteServerPublisher.java

protected void addRemoveProps(IPath deltaPath, IResource deltaResource, ZipOutputStream zip,
        Map<ZipEntry, String> deleteEntries, String deletePrefix) throws IOException {
    String archive = removeArchive(deltaPath.toPortableString());

    ZipEntry zipEntry = null;/* w  w  w .  j  a va  2  s  . co m*/

    // check to see if we already have an entry for this archive
    for (ZipEntry entry : deleteEntries.keySet()) {
        if (entry.getName().startsWith(archive)) {
            zipEntry = entry;
        }
    }

    if (zipEntry == null) {
        zipEntry = new ZipEntry(archive + "META-INF/" + deletePrefix + "-partialapp-delete.props"); //$NON-NLS-1$ //$NON-NLS-2$
    }

    String existingFiles = deleteEntries.get(zipEntry);

    // String file = encodeRemovedPath(deltaPath.toPortableString().substring(archive.length()));
    String file = deltaPath.toPortableString().substring(archive.length());

    if (deltaResource.getType() == IResource.FOLDER) {
        file += "/.*"; //$NON-NLS-1$
    }

    deleteEntries.put(zipEntry, (existingFiles != null ? existingFiles : StringPool.EMPTY) + (file + "\n")); //$NON-NLS-1$
}

From source file:de.nx42.maps4cim.util.Compression.java

/**
 * Compresses the input file using the zip file format and stores the
 * resulting zip file in the desired location
 * @param input the file to compress//from   w  w  w  .j ava  2  s  .  com
 * @param zipOutput the resulting zip file
 * @return the resulting zip file
 * @throws IOException if there is an error accessing the input file or
 * writing the output zip file
 */
public static File storeAsZip(File input, File zipOutput) throws IOException {
    // create new zip output stream
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipOutput));
    ZipEntry ze = new ZipEntry(input.getName());
    zos.putNextEntry(ze);

    // use file as input stream and copy bytes
    InputStream in = new FileInputStream(input);
    ByteStreams.copy(in, zos);

    // close current zip entry and all streams
    zos.closeEntry();
    in.close();
    zos.close();
    return zipOutput;
}

From source file:de.uni_hildesheim.sse.ant.versionReplacement.PluginVersionReplacer.java

/**
 * Recursive method for adding contents (also sub folders) of a folder to a Zip file.
 * Code comes from://from  w ww .  java2s .  co m
 * <tt>http://www.java2s.com/Code/Java/File-Input-Output/
 * Makingazipfileofdirectoryincludingitssubdirectoriesrecursively.htm</tt>.
 * @param basePath The base path of the Zip folder (for creating relative folders inside the Zip archive).
 * @param directory The current directory which shall be zipped into the Zip archive.
 * @param out A Zip archive.
 * @throws IOException if an I/O error has occurred
 */
private static void addDir(String basePath, File directory, ZipOutputStream out) throws IOException {
    File[] files = directory.listFiles();

    for (int i = 0; i < files.length; i++) {
        String entryName = makeRelative(basePath, files[i]);
        if (null != entryName && !entryName.isEmpty()) {
            if (files[i].isDirectory()) {
                out.putNextEntry(new ZipEntry(entryName));
                out.closeEntry();
                addDir(basePath, files[i], out);
            } else {
                out.putNextEntry(new ZipEntry(entryName));
                FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
                try {
                    IOUtils.copy(in, out);
                    in.close();
                } catch (IOException e1) {
                    IOUtils.closeQuietly(in);
                    throw e1;
                }
                out.closeEntry();
            }
        }
    }
}