Example usage for java.util.zip ZipOutputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the ZIP output stream as well as the stream being filtered.

Usage

From source file:es.sm2.openppm.core.plugin.action.GenericAction.java

/**
 *
 * @param files//from  www .  ja va2 s.  co m
 * @return
 * @throws IOException
 */
public static byte[] zipFiles(List<File> files) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);

    for (File f : files) {

        zos.putNextEntry(new ZipEntry(f.getName()));

        zos.write(getBytesFromFile(f.getAbsoluteFile()));

        zos.closeEntry();
    }

    zos.flush();
    baos.flush();
    zos.close();
    baos.close();

    return baos.toByteArray();
}

From source file:com.danimahardhika.android.helpers.core.FileHelper.java

@Nullable
public static String createZip(@NonNull List<String> files, File file) {
    try {//from www  .j  a v  a2  s .  co m
        final int BUFFER = 2048;
        BufferedInputStream origin;
        FileOutputStream dest = new FileOutputStream(file);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));

        byte data[] = new byte[BUFFER];
        for (int i = 0; i < files.size(); i++) {
            FileInputStream fi = new FileInputStream(files.get(i));
            origin = new BufferedInputStream(fi, BUFFER);

            ZipEntry entry = new ZipEntry(files.get(i).substring(files.get(i).lastIndexOf("/") + 1));
            out.putNextEntry(entry);
            int count;

            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }

        out.close();
        return file.toString();
    } catch (Exception ignored) {
    }
    return null;
}

From source file:com.dm.material.dashboard.candybar.helpers.FileHelper.java

public static void createZip(@NonNull List<String> files, String directory) {
    try {//from  w  w  w  .j a  v a2  s.  c  o m
        BufferedInputStream origin;
        FileOutputStream dest = new FileOutputStream(directory);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
        byte data[] = new byte[BUFFER];
        for (int i = 0; i < files.size(); i++) {
            FileInputStream fi = new FileInputStream(files.get(i));
            origin = new BufferedInputStream(fi, BUFFER);

            ZipEntry entry = new ZipEntry(files.get(i).substring(files.get(i).lastIndexOf("/") + 1));
            out.putNextEntry(entry);
            int count;

            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }

        out.close();
    } catch (Exception e) {
        LogUtil.e(Log.getStackTraceString(e));
    }
}

From source file:com.rackspacecloud.client.cloudfiles.sample.FilesCopy.java

public static File zipFile(File f) throws IOException {
    byte[] buf = new byte[1024];
    int len;/*from  ww  w . java 2  s  .  c om*/

    // Create the ZIP file
    String filenameWithZipExt = f.getName() + ZIPEXTENSION;
    File zippedFile = new File(FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filenameWithZipExt));

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile));
    FileInputStream in = new FileInputStream(f);

    // Add ZIP entry to output stream.
    out.putNextEntry(new ZipEntry(f.getName()));

    // Transfer bytes from the file to the ZIP file
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    // Complete the entry
    out.closeEntry();
    out.flush();

    in.close();
    out.close();

    return zippedFile;
}

From source file:Main.java

public static void zip(String zipFilePath, List<String> sourceFiles, String baseFolderPath, Boolean OverWrite)
        throws Exception {
    File zipFile = new File(zipFilePath);
    if (zipFile.exists() && !OverWrite)
        return;//  w ww  .  j av a2 s  .c  om
    else if (zipFile.exists() && OverWrite)
        zipFile.delete();

    ZipOutputStream zos = null;
    FileOutputStream outStream = null;
    outStream = new FileOutputStream(zipFile);
    zos = new ZipOutputStream(outStream);

    if (baseFolderPath == null)
        baseFolderPath = "";

    for (String item : sourceFiles) {
        String itemName = (item.startsWith(File.separator) || baseFolderPath.endsWith(File.separator)) ? item
                : (File.separator + item);

        if (baseFolderPath.equals("")) //absolute path
            addFileToZip(baseFolderPath + itemName, zos);
        else
            //relative path
            addFileToZip(baseFolderPath + itemName, baseFolderPath, zos);

    }
    zos.flush();
    zos.close();

}

From source file:com.chinamobile.bcbsp.fault.tools.Zip.java

/**
 * Compress files to *.zip.//from w  w  w  .  j  a  v a 2  s .  c o  m
 * @param fileName
 *        file name to compress
 * @return compressed file.
 */
public static String compress(String fileName) {
    String targetFile = null;
    File sourceFile = new File(fileName);
    Vector<File> vector = getAllFiles(sourceFile);
    try {
        if (sourceFile.isDirectory()) {
            targetFile = fileName + ".zip";
        } else {
            char ch = '.';
            targetFile = fileName.substring(0, fileName.lastIndexOf(ch)) + ".zip";
        }
        BufferedInputStream bis = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
        ZipOutputStream zipos = new ZipOutputStream(bos);
        byte[] data = new byte[BUFFER];
        if (vector.size() > 0) {
            for (int i = 0; i < vector.size(); i++) {
                File file = vector.get(i);
                zipos.putNextEntry(new ZipEntry(getEntryName(fileName, file)));
                bis = new BufferedInputStream(new FileInputStream(file));
                int count;
                while ((count = bis.read(data, 0, BUFFER)) != -1) {
                    zipos.write(data, 0, count);
                }
                bis.close();
                zipos.closeEntry();
            }
            zipos.close();
            bos.close();
            return targetFile;
        } else {
            File zipNullfile = new File(targetFile);
            zipNullfile.getParentFile().mkdirs();
            zipNullfile.mkdir();
            return zipNullfile.getAbsolutePath();
        }
    } catch (IOException e) {
        LOG.error("[compress]", e);
        return "error";
    }
}

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);// ww  w  .  ja va2  s  .  co  m
    zipOutputStream.closeEntry();
    zipOutputStream.close();
}

From source file:Main.java

/**
 * Zips a file at a location and places the resulting zip file at the toLocation.
 * <p/>/*  w w w .ja  va 2  s.c  o 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:ZipHandler.java

/**
 * makes a zip file named <xmlFileName> from <xmlURL> at path <zipURL>
 * @param zipURL//  ww w .java2 s.  c  om
 * @param xmlURL
 * @param xmlFileName
 */

public static void makeZip(String zipURL, String xmlURL, String xmlFileName) throws IOException {
    FileOutputStream fos = new FileOutputStream(new File(zipURL));
    ZipOutputStream zos = new ZipOutputStream(fos);
    //         zos.setLevel();
    FileInputStream fis = new FileInputStream(xmlURL);
    zos.putNextEntry(new ZipEntry(xmlFileName + ".xml"));
    writeInOutputStream(fis, zos);
    String bpath = "c:\\";
    FileInputStream fisBLOB = createInputStream(bpath);
    zos.putNextEntry(new ZipEntry("blob.lob"));
    writeInOutputStream(fisBLOB, zos);

    zos.closeEntry();
    String cpath = "c:\\";
    FileInputStream fisCLOB = createInputStream(cpath);
    zos.putNextEntry(new ZipEntry("clob.lob"));
    writeInOutputStream(fisCLOB, zos);
    zos.closeEntry();
    fis.close();
    fisCLOB.close();
    fisBLOB.close();
    zos.close();
    fos.close();

}

From source file:gov.va.chir.tagline.dao.FileDao.java

private static void saveZipFile(final File zipFile, final File... inputFiles) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];

    final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));

    for (File file : inputFiles) {
        final ZipEntry entry = new ZipEntry(file.getName());
        zos.putNextEntry(entry);//from w  w w .j a  v a  2 s  .c  o m

        final FileInputStream in = new FileInputStream(file);
        int len;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }

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

    zos.close();
}