Example usage for java.util.zip ZipOutputStream putNextEntry

List of usage examples for java.util.zip ZipOutputStream putNextEntry

Introduction

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

Prototype

public void putNextEntry(ZipEntry e) throws IOException 

Source Link

Document

Begins writing a new ZIP file entry and positions the stream to the start of the entry data.

Usage

From source file:fll.web.GatherBugReport.java

/**
 * Add the database to the zipfile./* w w  w . j a v  a  2s  .c  o  m*/
 * 
 * @throws SQLException
 */
private static void addDatabase(final ZipOutputStream zipOut, final Connection connection,
        final Document challengeDocument) throws IOException, SQLException {

    ZipOutputStream dbZipOut = null;
    FileInputStream fis = null;
    try {
        final File temp = File.createTempFile("database", ".flldb");

        dbZipOut = new ZipOutputStream(new FileOutputStream(temp));
        DumpDB.dumpDatabase(dbZipOut, connection, challengeDocument);
        dbZipOut.close();

        zipOut.putNextEntry(new ZipEntry("database.flldb"));
        fis = new FileInputStream(temp);
        IOUtils.copy(fis, zipOut);
        fis.close();

        if (!temp.delete()) {
            temp.deleteOnExit();
        }

    } finally {
        IOUtils.closeQuietly(dbZipOut);
        IOUtils.closeQuietly(fis);
    }

}

From source file:Main.java

/**
 * Zips a file at a location and places the resulting zip file at the toLocation.
 * <p/>//from w w  w.  j  a  v a  2s  . co  m
 * Example:
 * zipFileAtPath("downloads/myfolder", "downloads/myFolder.zip");
 * <p/>
 * http://stackoverflow.com/a/14868161
 */
public static void zipFileAtPath(String sourcePath, String toLocation) throws IOException {
    final int BUFFER = 2048;

    File sourceFile = new File(sourcePath);
    FileOutputStream fos = new FileOutputStream(toLocation);
    ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(fos));

    if (sourceFile.isDirectory()) {
        zipSubFolder(zipOut, sourceFile, sourceFile.getParent().length() + 1); // ??

    } else {
        byte data[] = new byte[BUFFER];
        FileInputStream fis = new FileInputStream(sourcePath);
        BufferedInputStream bis = new BufferedInputStream(fis, BUFFER);

        String lastPathComponent = sourcePath.substring(sourcePath.lastIndexOf("/"));

        ZipEntry zipEntry = new ZipEntry(lastPathComponent);
        zipOut.putNextEntry(zipEntry);

        int count;
        while ((count = bis.read(data, 0, BUFFER)) != -1) {
            zipOut.write(data, 0, count);
        }
    }

    zipOut.close();
}

From source file:io.druid.java.util.common.CompressionUtils.java

public static void makeEvilZip(File outputFile) throws IOException {
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(outputFile));
    ZipEntry zipEntry = new ZipEntry("../../../../../../../../../../../../../../../tmp/evil.txt");
    zipOutputStream.putNextEntry(zipEntry);
    byte[] output = StringUtils.toUtf8("evil text");
    zipOutputStream.write(output);// w  w w  . j  av  a2 s . c  o  m
    zipOutputStream.closeEntry();
    zipOutputStream.close();
}

From source file:com.nridge.core.base.std.FilUtl.java

/**
 * Compresses the input file into a ZIP file container.
 *
 * @param aInFileName The file name to be compressed.
 * @param aZipFileName The file name of the ZIP container.
 * @throws IOException Related to opening the file streams and
 * related read/write operations.//  w  w  w . j  av  a 2s.  co m
 */
static public void zipFile(String aInFileName, String aZipFileName) throws IOException {
    File inFile;
    int byteCount;
    byte[] ioBuf;
    FileInputStream fileIn;
    ZipOutputStream zipOut;
    FileOutputStream fileOut;

    inFile = new File(aInFileName);
    if (inFile.isDirectory())
        return;

    ioBuf = new byte[FILE_IO_BUFFER_SIZE];
    fileIn = new FileInputStream(inFile);
    fileOut = new FileOutputStream(aZipFileName);
    zipOut = new ZipOutputStream(fileOut);
    zipOut.putNextEntry(new ZipEntry(inFile.getName()));
    byteCount = fileIn.read(ioBuf);
    while (byteCount > 0) {
        zipOut.write(ioBuf, 0, byteCount);
        byteCount = fileIn.read(ioBuf);
    }
    fileIn.close();
    zipOut.closeEntry();
    zipOut.close();
}

From source file:Main.java

private static void compressDir(File srcFile, ZipOutputStream out, String destPath) throws IOException {
    if (srcFile.isDirectory()) {
        File subfile[] = srcFile.listFiles();
        for (int i = 0; i < subfile.length; i++) {
            compressDir(subfile[i], out, destPath);
        }//from w w  w . j av a 2s  . c  o  m
    } else {
        InputStream in = new FileInputStream(srcFile);
        String name = srcFile.getAbsolutePath().replace(destPath, "");
        if (name.startsWith("\\"))
            name = name.substring(1);
        ZipEntry entry = new ZipEntry(name);
        entry.setSize(srcFile.length());
        entry.setTime(srcFile.lastModified());
        out.putNextEntry(entry);
        int len = -1;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1)
            out.write(buf, 0, len);

        out.closeEntry();
        in.close();
    }
}

From source file:apim.restful.exportimport.utils.ArchiveGenerator.java

public static void addToZip(File directoryToZip, File file, ZipOutputStream zos)
        throws FileNotFoundException, IOException {

    FileInputStream fis = new FileInputStream(file);

    // we want the zipEntry's path to be a relative path that is relative
    // to the directory being zipped, so chop off the rest of the path
    String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
            file.getCanonicalPath().length());
    System.out.println("Writing '" + zipFilePath + "' to zip file");
    ZipEntry zipEntry = new ZipEntry(zipFilePath);
    zos.putNextEntry(zipEntry);

    byte[] bytes = new byte[1024];
    int length;/*from  w  w  w  . j  a  v a2s .c  o  m*/
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }

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

From source file:net.sourceforge.floggy.maven.ZipUtils.java

/**
 * DOCUMENT ME!//from   ww  w .j  a  v  a 2s  . c  o  m
*
* @param directories DOCUMENT ME!
* @param output DOCUMENT ME!
*
* @throws IOException DOCUMENT ME!
*/
public static void zip(File[] directories, File output) throws IOException {
    FileInputStream in = null;
    ZipOutputStream out = null;
    ZipEntry entry = null;

    Collection allFiles = new LinkedList();

    for (int i = 0; i < directories.length; i++) {
        allFiles.addAll(FileUtils.listFiles(directories[i], null, true));
    }

    out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(output)));

    for (Iterator iter = allFiles.iterator(); iter.hasNext();) {
        File temp = (File) iter.next();
        String name = getEntryName(directories, temp.getAbsolutePath());
        entry = new ZipEntry(name);
        out.putNextEntry(entry);

        if (!temp.isDirectory()) {
            in = new FileInputStream(temp);
            IOUtils.copy(in, out);
            in.close();
        }
    }

    out.close();
}

From source file:com.edgenius.core.util.ZipFileUtil.java

private static void addEntry(ZipOutputStream zop, File entry, String entryName) throws IOException {
    if (StringUtils.isBlank(entryName))
        return;//  w  w w  .  j  av a2s  .  c  o m

    BufferedInputStream source = null;
    if (entry != null) {
        source = new BufferedInputStream(new FileInputStream(entry));
    } else {
        //to make this as directory
        if (!entryName.endsWith("/"))
            entryName += "/";
    }
    ZipEntry zipEntry = new ZipEntry(entryName);
    zop.putNextEntry(zipEntry);

    if (entry != null) {
        //transfer bytes from file to ZIP file
        byte[] data = new byte[BUFFER_SIZE];
        int length;
        while ((length = source.read(data)) > 0) {
            zop.write(data, 0, length);
        }
        IOUtils.closeQuietly(source);
    }
    zop.closeEntry();

}

From source file:com.isomorphic.maven.util.ArchiveUtils.java

/**
 * Steps common to archiving both zip and jar files, which include reading files from disk and using
 * their contents to create {@link ZipEntry ZipEntries} and writing them to a ZipOutputStream.
 * //w w  w. ja  va2s  .  c o m
 * @param root
 * @param source
 * @param target
 * @throws IOException
 */
private static void zip(File root, File source, ZipOutputStream target) throws IOException {
    String relativePath = root.toURI().relativize(source.toURI()).getPath().replace("\\", "/");

    BufferedInputStream in = null;
    try {
        if (source.isDirectory()) {

            if (!relativePath.endsWith("/")) {
                relativePath += "/";
            }
            ZipEntry entry = ZipEntryFactory.get(target, relativePath);
            entry.setTime(source.lastModified());
            target.putNextEntry(entry);
            target.closeEntry();

            for (File nestedFile : source.listFiles()) {
                zip(root, nestedFile, target);
            }
            return;
        }

        ZipEntry entry = ZipEntryFactory.get(target, relativePath);
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        in = new BufferedInputStream(FileUtils.openInputStream(source));
        IOUtils.copy(in, target);
        target.closeEntry();

    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:edu.isi.wings.portal.classes.StorageHandler.java

private static void zipAndStream(File dir, ZipOutputStream zos, String prefix) throws Exception {
    byte bytes[] = new byte[2048];
    for (File file : dir.listFiles()) {
        if (file.isDirectory())
            StorageHandler.zipAndStream(file, zos, prefix + file.getName() + "/");
        else {//from  ww  w  .  ja  v  a  2 s  .  c  o  m
            FileInputStream fis = new FileInputStream(file.getAbsolutePath());
            BufferedInputStream bis = new BufferedInputStream(fis);
            zos.putNextEntry(new ZipEntry(prefix + file.getName()));
            int bytesRead;
            while ((bytesRead = bis.read(bytes)) != -1) {
                zos.write(bytes, 0, bytesRead);
            }
            zos.closeEntry();
            bis.close();
            fis.close();
        }
    }
}