Example usage for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream finish

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream finish

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream finish.

Prototype

public void finish() throws IOException 

Source Link

Usage

From source file:org.pepstock.jem.util.ZipUtil.java

/** 
  * Creates a zip output stream  at the specified path with the contents of the specified directory. 
  * //from  w  ww .  j a  va 2s. c om
  * @param folder folder to zip 
  * @param zipOutputStream output stream. Usually a bytearray 
  * @throws IOException if any error occurs 
  */
public static void createZip(File folder, OutputStream zipOutputStream) throws IOException {
    BufferedOutputStream bufferedOutputStream = null;
    ZipArchiveOutputStream zipArchiveOutputStream = null;
    try {
        bufferedOutputStream = new BufferedOutputStream(zipOutputStream);
        zipArchiveOutputStream = new ZipArchiveOutputStream(bufferedOutputStream);
        addFileToZip(zipArchiveOutputStream, folder);
    } finally {
        if (zipArchiveOutputStream != null) {
            zipArchiveOutputStream.finish();
            zipArchiveOutputStream.close();
        }
        if (bufferedOutputStream != null) {
            bufferedOutputStream.close();
        }
        if (zipOutputStream != null) {
            zipOutputStream.close();
        }
    }

}

From source file:org.sakaiproject.archive.impl.SiteZipper.java

/**
 * Zip a site archive. It is stored back in the zip directory
 * @param siteId         site that has already been archived
 * @param m_storagePath      path to where the archives are
 * @return/*from  w w w. j av a  2  s.c  o m*/
 * @throws IOException
 */
public boolean zipArchive(String siteId, String m_storagePath) throws IOException {

    //get path to archive dir for this site
    //suffix of -archive is hardcoded as per archive service
    String archivePath = m_storagePath + siteId + "-archive";

    //setup timestamp
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    String timestamp = dateFormat.format(Calendar.getInstance().getTime());

    //create path to compressed archive
    String compressedArchivePath = m_storagePath + siteId + "-" + timestamp + ".zip";
    File zipFile = new File(compressedArchivePath);

    if (!zipFile.exists()) {
        log.info("Creating zip file: " + compressedArchivePath);
        zipFile.createNewFile();
    }

    FileOutputStream fOut = null;
    FileInputStream zip = null;
    BufferedOutputStream bOut = null;
    ZipArchiveOutputStream zOut = null;

    try {
        fOut = new FileOutputStream(zipFile);
        bOut = new BufferedOutputStream(fOut);
        zOut = new ZipArchiveOutputStream(bOut);
        addFileToZip(zOut, archivePath, ""); //add the directory which will then add all files recursively

        //create a sha1 hash of the zip
        String hashPath = m_storagePath + siteId + "-" + timestamp + ".sha1";
        log.info("Creating hash: " + hashPath);
        zip = new FileInputStream(compressedArchivePath);
        String hash = DigestUtils.sha1Hex(zip);
        FileUtils.writeStringToFile(new File(hashPath), hash);
    } finally {
        zOut.finish();
        zOut.close();
        bOut.close();
        fOut.close();
        zip.close();
    }

    return true;
}

From source file:org.waarp.common.tar.ZipUtility.java

/**
 * Create a new Zip from a root directory
 * //from w  ww  . jav a  2 s. c om
 * @param directory
 *            the base directory
 * @param filename
 *            the output filename
 * @param absolute
 *            store absolute filepath (from directory) or only filename
 * @return True if OK
 */
public static boolean createZipFromDirectory(String directory, String filename, boolean absolute) {
    File rootDir = new File(directory);
    File saveFile = new File(filename);
    // recursive call
    ZipArchiveOutputStream zaos;
    try {
        zaos = new ZipArchiveOutputStream(new FileOutputStream(saveFile));
    } catch (FileNotFoundException e) {
        return false;
    }
    try {
        recurseFiles(rootDir, rootDir, zaos, absolute);
    } catch (IOException e2) {
        try {
            zaos.close();
        } catch (IOException e) {
            // ignore
        }
        return false;
    }
    try {
        zaos.finish();
    } catch (IOException e1) {
        // ignore
    }
    try {
        zaos.flush();
    } catch (IOException e) {
        // ignore
    }
    try {
        zaos.close();
    } catch (IOException e) {
        // ignore
    }
    return true;
}

From source file:org.waarp.common.tar.ZipUtility.java

/**
 * Create a new Zip from an array of Files (only name of files will be used)
 * //  w  w  w.  java 2 s  .  c o m
 * @param files
 *            array of files to add
 * @param filename
 *            the output filename
 * @return True if OK
 */
public static boolean createZipFromFiles(File[] files, String filename) {
    File saveFile = new File(filename);
    ZipArchiveOutputStream zaos;
    try {
        zaos = new ZipArchiveOutputStream(new FileOutputStream(saveFile));
    } catch (FileNotFoundException e) {
        return false;
    }
    for (File file : files) {
        try {
            addFile(file, zaos);
        } catch (IOException e) {
            try {
                zaos.close();
            } catch (IOException e1) {
                // ignore
            }
            return false;
        }
    }
    try {
        zaos.finish();
    } catch (IOException e1) {
        // ignore
    }
    try {
        zaos.flush();
    } catch (IOException e) {
        // ignore
    }
    try {
        zaos.close();
    } catch (IOException e) {
        // ignore
    }
    return true;
}

From source file:ThirdParty.zip.java

/**
 * Creates a zip file at the specified path with the contents of the specified directory.
 * @param directoryPath The path of the directory where the archive will be created. eg. c:/temp
 * @param zipPath The full path of the archive to create. eg. c:/temp/archive.zip
 * @throws IOException If anything goes wrong
 * @origin http://developer-tips.hubpages.com/hub/Zipping-and-Unzipping-Nested-Directories-in-Java-using-Apache-Commons-Compress
 * @date 2012-09-05/*ww  w.j  a  v  a 2  s  . co  m*/
 * @author Robin Spark
 */
public static void createZip(File directoryPath, File zipPath) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    ZipArchiveOutputStream tOut = null;

    try {
        fOut = new FileOutputStream(zipPath);
        bOut = new BufferedOutputStream(fOut);
        tOut = new ZipArchiveOutputStream(bOut);
        addFileToZip(tOut, directoryPath, "");
    } finally {
        tOut.finish();
        tOut.close();
        bOut.close();
        fOut.close();
    }

}