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:edu.kit.dama.util.ZipUtils.java

/**
 * Write all files located in pDirectory into a zip file specified by
 * pZipOut. The provided base path is used to keep the structure defined by
 * pDirectory within the zip file.//from   w w  w. ja va 2 s  .  c om
 *
 * Due to the fact, that we have only one single base path, pDirectory must
 * start with pBasePath to avoid unexpected zip file entries.
 *
 * @param pFiles The directory containing the input files
 * @param pBasePath The base path the will be removed from all file paths
 * before creating a new zip entry
 * @param pZipOut The zip output file
 * @throws IOException If something goes wrong, in most cases if there are
 * problems with reading the input files or writing into the output file
 */
public static void zip(File[] pFiles, String pBasePath, File pZipOut) throws IOException {
    ZipOutputStream zipOut = null;
    try {
        zipOut = new ZipOutputStream(new FileOutputStream(pZipOut));
        zip(pFiles, pBasePath, zipOut);
        zipOut.finish();
        zipOut.flush();
    } finally {
        try {
            if (zipOut != null) {
                zipOut.close();
            }
        } catch (IOException ignored) {
        }
    }
}

From source file:gov.nih.nci.caintegrator.common.Cai2Util.java

/**
 * Takes in a directory and zips it up./*from  w w  w .j a  v  a  2s .  c o m*/
 *
 * @param dir - Directory to zip.
 * @return Zipped file, stored in same location, with same name as the directory with .zip attached.
 * @throws IOException - In case cannot be read.
 */
public static File zipAndDeleteDirectory(String dir) throws IOException {
    File directory = new File(dir);
    if (!directory.isDirectory()) {
        throw new IllegalArgumentException("Not a directory:  " + dir);
    }
    int index = directory.getPath().indexOf(directory.getName());
    String[] entries = directory.list();
    if (entries.length == 0) {
        return null;
    }
    File zipfile = new File(dir + ZIP_FILE_SUFFIX);
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
    addDir(directory, out, index);
    out.close();
    FileUtils.deleteDirectory(directory);
    return zipfile;
}

From source file:com.eviware.soapui.testondemand.TestOnDemandCaller.java

private static byte[] zipBytes(String filename, byte[] dataToBeZiped) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zipedOutputStream = new ZipOutputStream(outputStream);
    ZipEntry entry = new ZipEntry(filename);
    entry.setSize(dataToBeZiped.length);
    try {/*from   w w w .j a  v a 2s . c  om*/
        zipedOutputStream.putNextEntry(entry);
        zipedOutputStream.write(dataToBeZiped);
    } finally {
        zipedOutputStream.closeEntry();
        zipedOutputStream.close();
    }
    return outputStream.toByteArray();
}

From source file:com.mcleodmoores.mvn.natives.UnpackDependenciesMojoTest.java

private static Artifact createArtifact(final File tmp, final String type, final String member)
        throws IOException {
    final File zipFile = new File(tmp, type + ".zip");
    try (final FileOutputStream out = new FileOutputStream(zipFile)) {
        final ZipOutputStream zipStream = new ZipOutputStream(out);
        final ZipEntry license = new ZipEntry("LICENSE");
        zipStream.putNextEntry(license);
        zipStream.write(26);//  w w w  .j av a  2s  .c o  m
        zipStream.closeEntry();
        final ZipEntry payload = new ZipEntry(member);
        zipStream.putNextEntry(payload);
        zipStream.write(26);
        zipStream.closeEntry();
        zipStream.close();
    }
    final Artifact artifact = Mockito.mock(Artifact.class);
    Mockito.when(artifact.getType()).thenReturn(type);
    Mockito.when(artifact.getGroupId()).thenReturn("uk.co.beerdragon");
    Mockito.when(artifact.getArtifactId()).thenReturn("test-" + type);
    Mockito.when(artifact.getVersion()).thenReturn("SNAPSHOT");
    Mockito.when(artifact.getFile()).thenReturn(zipFile);
    return artifact;
}

From source file:Main.java

/**
 * Compress files to a zip/*from   w w  w .ja va2  s.com*/
 * @param zip
 * @param files
 * @throws IOException 
 */
public static void compressToZip(File zip, File[] files) throws IOException {
    byte data[] = new byte[BUFFER];
    FileOutputStream fozip = new FileOutputStream(zip);
    ZipOutputStream zo = new ZipOutputStream(new BufferedOutputStream(fozip));
    for (int i = 0; i < files.length; i++) {
        System.out.println("Adding:" + files[i]);
        FileInputStream fi = new FileInputStream(files[i]);
        BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry zipentry = new ZipEntry(files[i].getName());
        zo.putNextEntry(zipentry);
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
            zo.write(data, 0, count);
        }
        origin.close();
    }
    zo.close();
}

From source file:azkaban.utils.Utils.java

public static void zipFolderContent(File folder, File output) throws IOException {
    FileOutputStream out = new FileOutputStream(output);
    ZipOutputStream zOut = new ZipOutputStream(out);
    try {//w w  w .  ja  v a 2 s. com
        File[] files = folder.listFiles();
        if (files != null) {
            for (File f : files) {
                zipFile("", f, zOut);
            }
        }
    } finally {
        zOut.close();
    }
}

From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.util.ZipUtils.java

/**
 * Builds the configuration ZIP file.//from w  ww.  j  ava2 s .co m
 * 
 * @param configName the configuration name
 * @param parentDir the parent directory
 * @return the file
 */
public static File buildConfigZip(final String configName, final File parentDir) {
    if (!parentDir.isDirectory()) {
        throw new RuntimeException(MSGS.format(CONFIG_NOT_DIR_1, parentDir.toString()));
    }

    final File zipFile = createEmptyZipFile(configName);
    final ZipOutputStream out;
    try {
        out = new ZipOutputStream(new FileOutputStream(zipFile));
    } catch (final FileNotFoundException e) {
        throw new RuntimeException(MSGS.format(ERROR_ZIPPING_1, parentDir.toString()), e);
    }

    try {
        addFilesToZip(parentDir, out, parentDir);
        return zipFile;
    } catch (final IOException e) {
        throw new RuntimeException(MSGS.format(ERROR_ZIPPING_1, parentDir.toString()), e);
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.gelakinetic.mtgfam.helpers.ZipUtils.java

/**
 * Zips all files specified in an ArrayList into a given file
 *
 * @param zipFile The file to zip files into
 * @param files   The files to be zipped
 * @param context The application context, for getting files and the like
 * @throws IOException IOException Thrown if something goes wrong with zipping and reading
 *//* ww  w . ja  v a 2  s  .co m*/
private static void zipIt(File zipFile, ArrayList<File> files, Context context) throws IOException {

    byte[] buffer = new byte[1024];

    FileOutputStream fos = new FileOutputStream(zipFile);
    ZipOutputStream zos = new ZipOutputStream(fos);
    assert context.getFilesDir() != null;
    for (File file : files) {
        ZipEntry ze = new ZipEntry(file.getName());
        zos.putNextEntry(ze);

        FileInputStream in = new FileInputStream(file);

        int len;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }
        in.close();
    }

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

From source file:com.datasalt.pangool.solr.TupleSolrOutputFormat.java

private static void createZip(File dir, File out) throws IOException {
    HashSet<File> files = new HashSet<File>();
    // take only conf/ and lib/
    for (String allowedDirectory : SolrRecordWriter.getAllowedConfigDirectories()) {
        File configDir = new File(dir, allowedDirectory);
        boolean configDirExists;
        /** If the directory does not exist, and is required, bail out */
        if (!(configDirExists = configDir.exists())
                && SolrRecordWriter.isRequiredConfigDirectory(allowedDirectory)) {
            throw new IOException(String.format("required configuration directory %s is not present in %s",
                    allowedDirectory, dir));
        }/*  w  w w .  ja  v a 2 s .com*/
        if (!configDirExists) {
            continue;
        }
        listFiles(configDir, files); // Store the files in the existing, allowed
                                     // directory configDir, in the list of files
                                     // to store in the zip file
    }

    out.delete();
    int subst = dir.toString().length();
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out));
    byte[] buf = new byte[1024];
    for (File f : files) {
        ZipEntry ze = new ZipEntry(f.toString().substring(subst));
        zos.putNextEntry(ze);
        InputStream is = new FileInputStream(f);
        int cnt;
        while ((cnt = is.read(buf)) >= 0) {
            zos.write(buf, 0, cnt);
        }
        is.close();
        zos.flush();
        zos.closeEntry();
    }
    zos.close();
}

From source file:dk.netarkivet.common.utils.ZipUtils.java

/** Zip the contents of a directory into a file.
 *  Does *not* zip recursively.//from w ww.  j av  a2 s. c o  m
 *
 * @param dir The directory to zip.
 * @param into The (zip) file to create.  The name should typically end
 * in .zip, but that is not required.
 */
public static void zipDirectory(File dir, File into) {
    ArgumentNotValid.checkNotNull(dir, "File dir");
    ArgumentNotValid.checkNotNull(into, "File into");
    ArgumentNotValid.checkTrue(dir.isDirectory(), "directory '" + dir + "' to zip is not a directory");
    ArgumentNotValid.checkTrue(into.getAbsoluteFile().getParentFile().canWrite(),
            "cannot write to '" + into + "'");

    File[] files = dir.listFiles();
    FileOutputStream out;
    try {
        out = new FileOutputStream(into);
    } catch (IOException e) {
        throw new IOFailure("Error creating ZIP outfile file '" + into + "'", e);
    }
    ZipOutputStream zipout = new ZipOutputStream(out);
    try {
        try {
            for (File f : files) {
                if (f.isFile()) {
                    ZipEntry entry = new ZipEntry(f.getName());
                    zipout.putNextEntry(entry);
                    FileUtils.writeFileToStream(f, zipout);
                } // Not doing directories yet.
            }
        } finally {
            zipout.close();
        }
    } catch (IOException e) {
        throw new IOFailure("Failed to zip directory '" + dir + "'", e);
    }
}