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

@Test
public void testZipSourceOneDirMultipleFilesAndGit() throws Exception {
    clearSourceDirectory();//w  w w . j a  v  a2 s.  c  o  m
    String buildSpecName = "buildspec.yml";
    String rootFileName = "pom.xml";
    String sourceDirName = "src";
    String srcFileName = "file.java";
    String srcFile2Name = "util.java";

    File buildSpec = new File("/tmp/source/" + buildSpecName);
    File rootFile = new File("/tmp/source/" + rootFileName);
    File sourceDir = new File("/tmp/source/" + sourceDirName);
    sourceDir.mkdir();
    File srcFile = new File("/tmp/source/src/" + srcFileName);
    File srcFile2 = new File("/tmp/source/src/" + srcFile2Name);

    File gitDir = new File("/tmp/source/.git");
    gitDir.mkdir();
    File gitFile = new File("/tmp/source/.git/something.txt");

    String rootFileContents = "<plugin>codebuild</plugin>";
    String buildSpecContents = "Hello!!!!!";
    String srcFileContents = "int i = 1;";
    String srcFile2Contents = "util() { ; }";
    String gitFileContents = "git data";

    FileUtils.write(buildSpec, buildSpecContents);
    FileUtils.write(rootFile, rootFileContents);
    FileUtils.write(srcFile, srcFileContents);
    FileUtils.write(srcFile2, srcFile2Contents);
    FileUtils.write(gitFile, gitFileContents);

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream("/tmp/source.zip"));
    S3DataManager dataManager = createDefaultSource();
    dataManager.zipSource("/tmp/source/", out, "/tmp/source/");
    out.close();

    File zip = new File("/tmp/source.zip");
    assertTrue(zip.exists());

    File unzipFolder = new File("/tmp/folder/");
    unzipFolder.mkdir();
    ZipFile z = new ZipFile(zip.getPath());
    z.extractAll(unzipFolder.getPath());
    assertTrue(unzipFolder.list().length == 3);
    File srcFolder = new File("/tmp/folder/src/");
    assertTrue(srcFolder.list().length == 2);
    List<String> files = Arrays.asList(unzipFolder.list());
    assertTrue(files.contains(buildSpecName));
    assertTrue(files.contains(rootFileName));
    assertTrue(files.contains(sourceDirName));
    assertFalse(files.contains("something.txt"));

    File extractedBuildSpec = new File(unzipFolder.getPath() + "/" + buildSpecName);
    File extractedRootFile = new File(unzipFolder.getPath() + "/" + rootFileName);
    File extractedSrcFile = new File(unzipFolder.getPath() + "/src/" + srcFileName);
    File extractedSrcFile2 = new File(unzipFolder.getPath() + "/src/" + srcFile2Name);
    assertTrue(FileUtils.readFileToString(extractedBuildSpec).equals(buildSpecContents));
    assertTrue(FileUtils.readFileToString(extractedRootFile).equals(rootFileContents));
    assertTrue(FileUtils.readFileToString(extractedSrcFile).equals(srcFileContents));
    assertTrue(FileUtils.readFileToString(extractedSrcFile2).equals(srcFile2Contents));
}

From source file:S3DataManagerTest.java

@Test
public void testZipSourceMultipleNestedDirs() throws Exception {
    clearSourceDirectory();//ww w .ja  va  2 s.  c  o m

    String buildSpecName = "buildspec.yml";
    String dir1Name = "dir1";
    String dir2Name = "dir2";
    String dir3Name = "dir3";
    String dir4Name = "dir4";
    String dir5Name = "dir5";
    String nestedFile4Name = "file.txt";
    String nestedFile5Name = "log.txt";

    File buildSpec = new File("/tmp/source/" + buildSpecName);
    File dir1 = new File("/tmp/source/" + dir1Name);
    dir1.mkdir();
    File dir2 = new File("/tmp/source/" + dir1Name + "/" + dir2Name);
    dir2.mkdir();
    File dir3 = new File("/tmp/source/" + dir1Name + "/" + dir2Name + "/" + dir3Name);
    dir3.mkdir();
    File dir4 = new File("/tmp/source/dir1/dir2/dir3/" + dir4Name);
    dir4.mkdir();
    File dir5 = new File("/tmp/source/dir1/dir2/dir3/" + dir5Name);
    dir5.mkdir();
    File file4 = new File("/tmp/source/dir1/dir2/dir3/dir4/" + nestedFile4Name);
    File file5 = new File("/tmp/source/dir1/dir2/dir3/dir5/" + nestedFile5Name);

    String buildSpecContents = "Hello!!!!!";
    String nestedFile4Contents = "words";
    String nestedFile5Contents = "logfile";
    FileUtils.write(buildSpec, buildSpecContents);
    FileUtils.write(file4, nestedFile4Contents);
    FileUtils.write(file5, nestedFile5Contents);

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream("/tmp/source.zip"));
    S3DataManager dataManager = createDefaultSource();
    dataManager.zipSource("/tmp/source/", out, "/tmp/source/");
    out.close();

    File zip = new File("/tmp/source.zip");
    assertTrue(zip.exists());

    File unzipFolder = new File("/tmp/folder/");
    unzipFolder.mkdir();
    ZipFile z = new ZipFile(zip.getPath());
    z.extractAll(unzipFolder.getPath());

    unzipFolder = new File("/tmp/folder/" + dir1Name);
    assertTrue(unzipFolder.list().length == 1);
    assertTrue(unzipFolder.list()[0].equals(dir2Name));

    unzipFolder = new File("/tmp/folder/" + dir1Name + "/" + dir2Name);
    assertTrue(unzipFolder.list().length == 1);
    assertTrue(unzipFolder.list()[0].equals(dir3Name));

    unzipFolder = new File("/tmp/folder/dir1/dir2/dir3/");
    assertTrue(unzipFolder.list().length == 2);

    unzipFolder = new File("/tmp/folder/dir1/dir2/dir3/dir4/");
    assertTrue(unzipFolder.list().length == 1);
    assertTrue(unzipFolder.list()[0].equals(nestedFile4Name));

    unzipFolder = new File("/tmp/folder/dir1/dir2/dir3/dir5/");
    assertTrue(unzipFolder.list().length == 1);
    assertTrue(unzipFolder.list()[0].equals(nestedFile5Name));
}

From source file:S3DataManagerTest.java

@Test
public void testZipSourceDoubleDirMultipleHugeFiles() throws Exception {
    clearSourceDirectory();//from w w w .  j a  v a2s.  c om
    String buildSpecName = "buildspec.yml";
    String rootFileName = "pom.xml";
    String sourceDirName = "src";
    String nestedDirName = "dir";
    String srcFileName = "file.java";
    String srcFile2Name = "util.java";

    File buildSpec = new File("/tmp/source/" + buildSpecName);
    File rootFile = new File("/tmp/source/" + rootFileName);
    File sourceDir = new File("/tmp/source/" + sourceDirName);
    sourceDir.mkdir();
    File nestedDir = new File("/tmp/source/" + sourceDirName + "/" + nestedDirName);
    nestedDir.mkdir();
    File srcFile = new File("/tmp/source/src/" + srcFileName);
    File srcFile2 = new File("/tmp/source/src/dir/" + srcFile2Name);

    StringBuilder rootFileContents = new StringBuilder();
    for (int i = 0; i < 1000; i++) {
        rootFileContents.append("word\n");
    }
    String buildSpecContents = "Hello!!!!!";
    String srcFileContents = "int i = 1;";
    StringBuilder srcFile2Contents = new StringBuilder();
    for (int i = 0; i < 2000; i++) {
        srcFile2Contents.append("function()");
    }

    FileUtils.write(buildSpec, buildSpecContents);
    FileUtils.write(rootFile, rootFileContents.toString());
    FileUtils.write(srcFile, srcFileContents);
    FileUtils.write(srcFile2, srcFile2Contents.toString());

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream("/tmp/source.zip"));
    S3DataManager dataManager = createDefaultSource();
    dataManager.zipSource("/tmp/source/", out, "/tmp/source/");
    out.close();

    File zip = new File("/tmp/source.zip");
    assertTrue(zip.exists());

    File unzipFolder = new File("/tmp/folder/");
    unzipFolder.mkdir();
    ZipFile z = new ZipFile(zip.getPath());
    z.extractAll(unzipFolder.getPath());
    assertTrue(unzipFolder.list().length == 3);
    File srcFolder = new File("/tmp/folder/src/");
    assertTrue(srcFolder.list().length == 2);
    File nestedFolder = new File("/tmp/folder/src/dir/");
    assertTrue(nestedFolder.list().length == 1);
    List<String> files = Arrays.asList(unzipFolder.list());
    assertTrue(files.contains(buildSpecName));
    assertTrue(files.contains(rootFileName));
    assertTrue(files.contains(sourceDirName));
    assertTrue(nestedFolder.list()[0].equals(srcFile2Name));

    File extractedBuildSpec = new File(unzipFolder.getPath() + "/" + buildSpecName);
    File extractedRootFile = new File(unzipFolder.getPath() + "/" + rootFileName);
    File extractedSrcFile = new File(unzipFolder.getPath() + "/src/" + srcFileName);
    File extractedSrcFile2 = new File(unzipFolder.getPath() + "/src/dir/" + srcFile2Name);
    assertTrue(FileUtils.readFileToString(extractedBuildSpec).equals(buildSpecContents));
    assertTrue(FileUtils.readFileToString(extractedRootFile).equals(rootFileContents.toString()));
    assertTrue(FileUtils.readFileToString(extractedSrcFile).equals(srcFileContents));
    assertTrue(FileUtils.readFileToString(extractedSrcFile2).equals(srcFile2Contents.toString()));
}

From source file:freenet.client.async.ContainerInserter.java

private String createZipBucket(OutputStream os) throws IOException {
    if (logMINOR)
        Logger.minor(this, "Create a ZIP Bucket");

    ZipOutputStream zos = new ZipOutputStream(os);
    try {//from  ww w  . j  a  v  a 2 s . c  o m
        ZipEntry ze;

        for (ContainerElement ph : containerItems) {
            ze = new ZipEntry(ph.targetInArchive);
            ze.setTime(0);
            zos.putNextEntry(ze);
            BucketTools.copyTo(ph.data, zos, ph.data.size());
            zos.closeEntry();
        }
    } finally {
        zos.close();
    }

    return ARCHIVE_TYPE.ZIP.mimeTypes[0];
}

From source file:com.polyvi.xface.extension.zip.XZipExt.java

/**
 * ?/*from  w w w .jav  a2 s .c om*/
 *
 * @param srcEntries
 *            ????
 * @param destZipFile
 *            ??test.zip?a/b/c/test.zip
 * @return ??
 * @throws FileNotFoundException
 * @throws IOException
 * @throws IllegalArgumentException
 * @throws JSONException
 */
private void zipFiles(JSONArray srcEntries, String destZipFile)
        throws FileNotFoundException, IOException, IllegalArgumentException, JSONException {
    Uri destFileUri = getFileUri(destZipFile);
    prepareForZipDir(destFileUri);

    ZipOutputStream zos = new ZipOutputStream(mResourceApi.openOutputStream(destFileUri));
    Uri[] paths = new Uri[srcEntries.length()];
    String path = null;
    for (int i = 0; i < srcEntries.length(); i++) {
        path = srcEntries.getString(i);
        paths[i] = getFileUri(path);
        compressDir(paths[i], zos, "");
    }
    zos.close();
}

From source file:it.eng.spagobi.tools.scheduler.dispatcher.MailDocumentDispatchChannel.java

private MimeBodyPart zipAttachment(byte[] attach, String containedFileName, String zipFileName,
        String nameSuffix, String fileExtension) {
    MimeBodyPart messageBodyPart = null;
    try {//from   w ww . ja v  a 2 s.  c  o m

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ZipOutputStream zipOut = new ZipOutputStream(bout);
        String entryName = containedFileName + nameSuffix + fileExtension;
        zipOut.putNextEntry(new ZipEntry(entryName));
        zipOut.write(attach);
        zipOut.closeEntry();

        zipOut.close();

        messageBodyPart = new MimeBodyPart();
        DataSource source = new ByteArrayDataSource(bout.toByteArray(), "application/zip");
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(zipFileName + nameSuffix + ".zip");

    } catch (Exception e) {
        // TODO: handle exception            
    }
    return messageBodyPart;
}

From source file:com.joliciel.talismane.extensions.corpus.PosTaggerStatistics.java

@Override
public void onCompleteAnalysis() {
    try {/* w w  w  .  j  a va  2 s.c o  m*/
        if (writer != null) {
            PosTagSet posTagSet = talismaneSession.getPosTagSet();
            for (PosTag posTag : posTagSet.getTags()) {
                if (!posTagCounts.containsKey(posTag.getCode())) {
                    posTagCounts.put(posTag.getCode(), 0);
                }
            }

            double unknownLexiconPercent = 1;
            if (referenceWords != null) {
                int unknownLexiconCount = 0;
                for (String word : words) {
                    if (!referenceWords.contains(word))
                        unknownLexiconCount++;
                }
                unknownLexiconPercent = (double) unknownLexiconCount / (double) words.size();
            }
            double unknownLowercaseLexiconPercent = 1;
            if (referenceLowercaseWords != null) {
                int unknownLowercaseLexiconCount = 0;
                for (String lowercase : lowerCaseWords) {
                    if (!referenceLowercaseWords.contains(lowercase))
                        unknownLowercaseLexiconCount++;
                }
                unknownLowercaseLexiconPercent = (double) unknownLowercaseLexiconCount
                        / (double) lowerCaseWords.size();
            }

            writer.write(CSV.format("sentenceCount") + CSV.format(sentenceCount) + "\n");
            writer.write(CSV.format("sentenceLengthMean") + CSV.format(sentenceLengthStats.getMean()) + "\n");
            writer.write(CSV.format("sentenceLengthStdDev")
                    + CSV.format(sentenceLengthStats.getStandardDeviation()) + "\n");
            writer.write(CSV.format("lexiconSize") + CSV.format(words.size()) + "\n");
            writer.write(
                    CSV.format("lexiconUnknownInRefCorpus") + CSV.format(unknownLexiconPercent * 100.0) + "\n");
            writer.write(CSV.format("tokenCount") + CSV.format(tokenCount) + "\n");

            double unknownTokenPercent = ((double) unknownTokenCount / (double) tokenCount) * 100.0;
            writer.write(CSV.format("tokenUnknownInRefCorpus") + CSV.format(unknownTokenPercent) + "\n");

            double unknownInLexiconPercent = ((double) unknownInLexiconCount / (double) tokenCount) * 100.0;
            writer.write(CSV.format("tokenUnknownInRefLexicon") + CSV.format(unknownInLexiconPercent) + "\n");

            writer.write(CSV.format("lowercaseLexiconSize") + CSV.format(lowerCaseWords.size()) + "\n");
            writer.write(CSV.format("lowercaseLexiconUnknownInRefCorpus")
                    + CSV.format(unknownLowercaseLexiconPercent * 100.0) + "\n");
            writer.write(CSV.format("alphanumericCount") + CSV.format(alphanumericCount) + "\n");

            double unknownAlphanumericPercent = ((double) unknownAlphanumericCount / (double) alphanumericCount)
                    * 100.0;
            writer.write(CSV.format("alphaUnknownInRefCorpus") + CSV.format(unknownAlphanumericPercent) + "\n");

            double unknownAlphaInLexiconPercent = ((double) unknownAlphaInLexiconCount
                    / (double) alphanumericCount) * 100.0;
            writer.write(
                    CSV.format("alphaUnknownInRefLexicon") + CSV.format(unknownAlphaInLexiconPercent) + "\n");

            writer.write(CSV.format("openClassCount") + CSV.format(openClassCount) + "\n");

            double openClassUnknownPercent = ((double) openClassUnknownInRefCorpus / (double) openClassCount)
                    * 100.0;
            writer.write(
                    CSV.format("openClassUnknownInRefCorpus") + CSV.format(openClassUnknownPercent) + "\n");

            double openClassUnknownInLexiconPercent = ((double) openClassUnknownInLexicon
                    / (double) openClassCount) * 100.0;
            writer.write(CSV.format("openClassUnknownInRefLexicon")
                    + CSV.format(openClassUnknownInLexiconPercent) + "\n");

            writer.write(CSV.format("closedClassCount") + CSV.format(closedClassCount) + "\n");

            double closedClassUnknownPercent = ((double) closedClassUnknownInRefCorpus
                    / (double) closedClassCount) * 100.0;
            writer.write(
                    CSV.format("closedClassUnknownInRefCorpus") + CSV.format(closedClassUnknownPercent) + "\n");

            double closedClassUnknownInLexiconPercent = ((double) closedClassUnknownInLexicon
                    / (double) closedClassCount) * 100.0;
            writer.write(CSV.format("closedClassUnknownInRefLexicon")
                    + CSV.format(closedClassUnknownInLexiconPercent) + "\n");

            for (String posTag : posTagCounts.keySet()) {
                int count = posTagCounts.get(posTag);
                writer.write(CSV.format(posTag) + CSV.format(count)
                        + CSV.format(((double) count / (double) tokenCount) * 100.0) + "\n");
            }

            writer.flush();
            writer.close();
        }

        if (this.serializationFile != null) {
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(serializationFile, false));
            zos.putNextEntry(new ZipEntry("Contents.obj"));
            ObjectOutputStream oos = new ObjectOutputStream(zos);
            try {
                oos.writeObject(this);
            } finally {
                oos.flush();
            }
            zos.flush();
            zos.close();
        }
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }

}

From source file:de.unisaarland.swan.export.ExportUtil.java

/**
 * Returns a zip file containing one XMI per document, UIMA annotations are
 * created for each document for all annotators that were assigned to the
 * project./*w w w.ja v  a  2  s. c  o  m*/
 *
 * @param proj Project
 * @return zip file
 */
public File getExportDataInXMI(Project proj) {
    try {
        File zipFile = new File("swan_" + proj.getName() + ".zip");
        ZipOutputStream zos = createZipOutputStream(zipFile);

        for (de.unisaarland.swan.entities.Document d : proj.getDocuments()) {
            File xmiFile = createXMIFileForDocument(d);
            createZipEntry(xmiFile, zos);
        }

        // Add type system for convenience
        File typeSystemFile = createTypeSystemFile();
        createZipEntry(typeSystemFile, zos);

        zos.close();

        return zipFile;
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ExportUtil.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UIMAException | SAXException | IOException ex) {
        Logger.getLogger(ExportUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

    throw new RuntimeException("ExportUtil: Error while creating UIMA XMI files / zipping");
}

From source file:nl.imvertor.common.file.ZipFile.java

/**
 * Zip it/*from www.  j a  v  a2s.co  m*/
 * @throws IOException 
 */
// adapted from http://www.mkyong.com/java/how-to-compress-files-in-zip-format/
private void zipIt() throws IOException {
    byte[] buffer = new byte[1024];
    FileOutputStream fos = new FileOutputStream(this);
    ZipOutputStream zos = new ZipOutputStream(fos);
    for (String file : this.fileList) {
        ZipEntry ze = new ZipEntry(file);
        zos.putNextEntry(ze);
        FileInputStream in = new FileInputStream(sourceFolder + File.separator + file);
        int len;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }
        in.close();
    }
    zos.closeEntry();
    zos.close();
}