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:Main.java

/**
 * Target is the zip to create// w  ww .j a  v a2  s.  co m
 * Source is the file or directory to zip
 * 
 * @param zipTarget
 * @param source
 * @param m
 * @throws IOException
 */
static public void compressDirectory(File zipTarget, File source) throws IOException {

    // create a ZipOutputStream to zip the data to.
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipTarget));
    try {
        if (source.isDirectory()) {
            zipDir(source, zos, source.getName());
        } else
            zipFile(source, zos, source.getName());
    } finally {// close the stream
        zos.close();
    }
}

From source file:com.ecofactor.qa.automation.platform.util.FileUtil.java

/**
 * Add a folder to Zip folder./* w  w  w  .  ja  va 2  s .c o m*/
 * @param srcFolder the src folder
 * @param destZipFile the dest zip file
 */
public static void zipFolder(final String srcFolder, final String destZipFile) {

    try {
        final FileOutputStream fileWriter = new FileOutputStream(destZipFile);
        final ZipOutputStream zip = new ZipOutputStream(fileWriter);
        addFolderToZip("", srcFolder, zip);
        zip.flush();
        zip.close();
    } catch (Exception e) {
        LOGGER.error("Error in creating zip file", e);
    }
}

From source file:brut.directory.ZipUtils.java

public static void zipFolders(final File folder, final File zip, final File assets,
        final Collection<String> doNotCompress) throws BrutException, IOException {

    mDoNotCompress = doNotCompress;//w w  w  .  j av a 2  s. c om
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zip));
    zipFolders(folder, zipOutputStream);

    // We manually set the assets because we need to retain the folder structure
    if (assets != null) {
        processFolder(assets, zipOutputStream, assets.getPath().length() - 6);
    }
    zipOutputStream.close();
}

From source file:Main.java

/**
 * Compress a String to a zip file that has only one entry like zipName
 * The name shouldn't have .zip/*  w  w  w .ja  va2  s  .  c  om*/
 * 
 * @param content
 * @param fName
 * @throws IOException 
 */
public static void zipAContentAsFileName(String fName, String content, String charset) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes(charset));
    BufferedInputStream bis = new BufferedInputStream(bais);

    File f = new File(fName);
    File parentFile = f.getParentFile();
    if (!parentFile.exists()) {
        parentFile.mkdirs();
    }
    ZipOutputStream zf = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f + ".zip")));

    ZipEntry entry = new ZipEntry(f.getName());
    zf.putNextEntry(entry);
    byte[] barr = new byte[8192];
    int len = 0;
    while ((len = bis.read(barr)) != -1) {
        zf.write(barr, 0, len);
    }
    zf.flush();
    zf.close();
    bis.close();
    bais.close();
}

From source file:com.twinsoft.convertigo.engine.util.ZipUtils.java

public static void makeZip(String archiveFileName, String sDir, String sRelativeDir, List<File> excludedFiles)
        throws Exception {
    FileOutputStream fos = new FileOutputStream(archiveFileName);
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
    int nbZipEntries = ZipUtils.putEntries(zos, sDir, sRelativeDir, excludedFiles);
    if (nbZipEntries > 0)
        zos.close();
}

From source file:ZipHandler.java

/**
 * makes a zip file named <xmlFileName> from <xmlURL> at path <zipURL>
 * @param zipURL/*w w w  .ja  v  a2s  . co  m*/
 * @param xmlURL
 * @param xmlFileName
 */

public static void makeStructZip(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);
    zos.closeEntry();
    fis.close();
    zos.close();
}

From source file:com.twinsoft.convertigo.engine.util.ZipUtils.java

public static void makeZip(String archiveFileName, String sDir, String sRelativeDir) throws Exception {
    FileOutputStream fos = new FileOutputStream(archiveFileName);
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
    int nbZipEntries = ZipUtils.putEntries(zos, sDir, sRelativeDir, Collections.<File>emptyList());
    if (nbZipEntries > 0)
        zos.close();
}

From source file:com.ikanow.aleph2.analytics.storm.utils.TestStormControllerUtil_Cache.java

private static File createFakeZipFile(String file_name) throws IOException {
    File file;/*from   ww w.  j  a  v  a2  s. c  o m*/
    if (file_name == null)
        file = File.createTempFile("recent_date_test_", ".zip");
    else
        file = new File(file_name);
    Random r = new Random();
    ZipOutputStream outputZip = new ZipOutputStream(new FileOutputStream(file));
    ZipEntry e = new ZipEntry("some_file.tmp");
    outputZip.putNextEntry(e);
    outputZip.write(r.nextInt());
    outputZip.close();
    return file;
}

From source file:Main.java

public static void compressFile(File file, File fileCompressed) throws IOException {

    byte[] buffer = new byte[SIZE_OF_BUFFER];
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed));
    FileInputStream fileInputStream = new FileInputStream(file);
    zipOutputStream.putNextEntry(new ZipEntry(file.getPath()));

    int size;/*from   w w w .j  a v  a  2s  .  c om*/
    while ((size = fileInputStream.read(buffer)) > 0)
        zipOutputStream.write(buffer, 0, size);

    zipOutputStream.closeEntry();
    fileInputStream.close();
    zipOutputStream.close();
}

From source file:Main.java

public static boolean compress(File file) {
    try {//from   ww  w. j a va  2s  . c  o m
        String fileName = file.getName();
        if (fileName.indexOf(".") != -1)
            fileName = fileName.substring(0, fileName.indexOf("."));
        FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName + ".zip");
        CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
        InputStream in = new FileInputStream(file);
        out.putNextEntry(new ZipEntry(file.getName()));
        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();
        out.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}