Example usage for java.util.zip ZipOutputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for writing the next entry.

Usage

From source file:game.com.HandleDownloadFolderServlet.java

private void outputZipStream(ZipOutputStream output, File file) throws FileNotFoundException, IOException {
    for (File currentFile : file.listFiles()) {

        InputStream input = null;
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        try {//from   w w w. j a  v a2 s  .c o m
            if (currentFile.isDirectory()) {
                outputZipStream(output, currentFile);
            } else {
                input = new BufferedInputStream(new FileInputStream(currentFile), DEFAULT_BUFFER_SIZE);
                output.putNextEntry(new ZipEntry(currentFile.getName()));
                for (int length = 0; (length = input.read(buffer)) > 0;) {
                    output.write(buffer, 0, length);
                }
                output.closeEntry();
            }
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (Exception logOrIgnore) {
                    logger.error(logOrIgnore.getMessage(), logOrIgnore);
                }
            }
        }
    }
}

From source file:com.bibisco.manager.ProjectManager.java

public static void zipIt(String zipFile) {

    mLog.debug("Start zipIt(String)");

    ContextManager lContextManager = ContextManager.getInstance();

    String lStrDBDirectoryPath = lContextManager.getDbDirectoryPath();
    String lStrDbProjectDirectory = getDBProjectDirectory(lContextManager.getIdProject());
    List<String> lFileList = getDirectoryFileList(new File(lStrDbProjectDirectory));

    try {//from w ww  .  j  a  v a  2  s. c  o  m
        File lFile = new File(zipFile);

        lFile.createNewFile();
        FileOutputStream lFileOutputStream = new FileOutputStream(lFile);
        ZipOutputStream lZipOutputStream = new ZipOutputStream(lFileOutputStream);

        byte[] buffer = new byte[1024];
        for (String lStrFile : lFileList) {

            ZipEntry lZipEntry = new ZipEntry(
                    lStrFile.substring(lStrDBDirectoryPath.length(), lStrFile.length()));
            lZipOutputStream.putNextEntry(lZipEntry);

            FileInputStream lFileInputStream = new FileInputStream(lStrFile);

            int lIntLen;
            while ((lIntLen = lFileInputStream.read(buffer)) > 0) {
                lZipOutputStream.write(buffer, 0, lIntLen);
            }

            lFileInputStream.close();
        }

        lZipOutputStream.closeEntry();
        lZipOutputStream.close();

        mLog.debug("Folder successfully compressed");
    } catch (IOException e) {
        mLog.error(e);
        throw new BibiscoException(e, BibiscoException.IO_EXCEPTION);
    }

    mLog.debug("End zipIt(String)");
}

From source file:org.alfresco.repo.web.scripts.custommodel.CustomModelImportTest.java

private File createZip(ZipEntryContext... zipEntryContexts) {
    File zipFile = TempFileProvider.createTempFile(getClass().getSimpleName(), ".zip");
    tempFiles.add(zipFile);/*from   ww  w .jav  a2 s .c o m*/

    byte[] buffer = new byte[BUFFER_SIZE];
    try {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(zipFile), BUFFER_SIZE);
        ZipOutputStream zos = new ZipOutputStream(out);

        for (ZipEntryContext context : zipEntryContexts) {
            ZipEntry zipEntry = new ZipEntry(context.getZipEntryName());
            zos.putNextEntry(zipEntry);

            InputStream input = context.getEntryContent();
            int len;
            while ((len = input.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
            input.close();
        }
        zos.closeEntry();
        zos.close();
    } catch (IOException ex) {
        fail("couldn't create zip file.");
    }

    return zipFile;
}

From source file:org.pdfgal.pdfgalweb.utils.impl.ZipUtilsImpl.java

/**
 * Adds a new file to the {@link ZipOutputStream}.
 * /* w  w w  . j  a v a2  s.  c o  m*/
 * @param fileName
 * @param zos
 * @param originalFileName
 * @throws FileNotFoundException
 * @throws IOException
 */
private void addToZipFile(final String fileName, final ZipOutputStream zos, final String originalFileName)
        throws IOException {

    // File is opened.
    final File file = new File(fileName);

    // File is renamed.
    final String newName = fileName.substring(fileName.indexOf(originalFileName), fileName.length());
    final File renamedFile = new File(newName);
    file.renameTo(renamedFile);

    // File is included into ZIP.
    final FileInputStream fis = new FileInputStream(renamedFile);
    final ZipEntry zipEntry = new ZipEntry(newName);
    zos.putNextEntry(zipEntry);

    final byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }

    // Closing elements.
    zos.closeEntry();
    fis.close();

    // File is deleted
    this.fileUtils.delete(newName);
}

From source file:de.bps.course.nodes.ChecklistCourseNode.java

@Override
public boolean archiveNodeData(Locale locale, ICourse course, ArchiveOptions options,
        ZipOutputStream exportStream, String charset) {
    String filename = "checklist_" + StringHelper.transformDisplayNameToFileSystemName(getShortName()) + "_"
            + Formatter.formatDatetimeFilesystemSave(new Date(System.currentTimeMillis()));

    Checklist checklist = loadOrCreateChecklist(course.getCourseEnvironment().getCoursePropertyManager());
    String exportContent = XStreamHelper.createXStreamInstance().toXML(checklist);
    try {//from ww  w  .ja v  a  2 s.c om
        exportStream.putNextEntry(new ZipEntry(filename));
        IOUtils.write(exportContent, exportStream);
        exportStream.closeEntry();
    } catch (IOException e) {
        log.error("", e);
    }
    return true;
}

From source file:org.bdval.ConsensusBDVModel.java

/**
 * Save the juror models to a set of files. The files will contain all the information needed
 * to apply the BDVal model to new samples.
 *
 * @param zipStream The stream used to write the models to
 * @param options The options associated with this model
 * @param task The classification task used for this model
 * @param splitPlan The split plan used to generat this model
 * @param writeModelMode The mode saving the model
 * @throws IOException if there is a problem writing to the files
 *//*ww  w .  j  a v  a2  s .  co  m*/
private synchronized void saveJurorModels(final ZipOutputStream zipStream, final DAVOptions options,
        final ClassificationTask task, final SplitPlan splitPlan, final WriteModel writeModelMode)
        throws IOException {
    if (!jurorModelsAreLoaded) {
        throw new IllegalStateException("juror models must be loaded before save");
    }

    // add the models directory entry for juror models (must end in "/")
    zipStream.putNextEntry(new ZipEntry(JUROR_MODEL_DIRECTORY + "/"));
    zipStream.closeEntry();

    final String jurorModelDirectory = JUROR_MODEL_DIRECTORY + "/"
            + FilenameUtils.getBaseName(FilenameUtils.getPathNoEndSeparator(modelFilenamePrefix)) + "/";
    zipStream.putNextEntry(new ZipEntry(jurorModelDirectory));
    zipStream.closeEntry();

    for (final BDVModel jurorModel : jurorModels) {
        // NOTE: for zip files, the directory MUST use "/" regardless of OS

        final String jurorModelFilename = jurorModelDirectory
                + FilenameUtils.getName(jurorModel.getModelFilenamePrefix()) + ".zip";

        // Add ZIP entry for the model to output stream.
        zipStream.putNextEntry(new ZipEntry(jurorModelFilename));

        // Create the ZIP file for the juror
        LOG.debug("Writing juror model as entry: " + jurorModelFilename);
        final ZipOutputStream jurorZipStream = new ZipOutputStream(zipStream);
        jurorZipStream.setComment("Juror model: " + jurorModel.getModelFilenamePrefix());
        jurorModel.save(jurorZipStream, options, task, splitPlan, writeModelMode);
        jurorZipStream.finish(); // NOTE: we don't close the stream, that will close everything

        zipStream.closeEntry();
    }
}

From source file:edu.dfci.cccb.mev.dataset.rest.controllers.WorkspaceController.java

private void zipAnnotations(String name, String dimension, ZipOutputStream zout)
        throws DatasetNotFoundException, IOException {
    Dataset dataset = workspace.get(name);
    long projectId = projectManager.getProjectID(dataset.name() + dimension);
    if (projectId > -1) {
        File annotations = new TemporaryFile();
        GZIPOutputStream gos = new GZIPOutputStream(new FileOutputStream(annotations));
        TarOutputStream tos = new TarOutputStream(gos);
        projectManager.exportProject(projectId, tos);
        tos.flush();//w  ww .  ja v  a 2 s  . c  om
        tos.close();
        zout.putNextEntry(new ZipEntry(String.format("annotations_%s.tar.gz", dimension)));
        IOUtils.copy(new FileInputStream(annotations), zout);
        zout.closeEntry();
    }
}

From source file:com.centurylink.mdw.common.service.JsonExport.java

/**
 * Default behavior adds zip entries for each property on the first (non-mdw) top-level object.
 *//*from w ww  . jav a  2  s .  com*/
public String exportZipBase64() throws JSONException, IOException {
    Map<String, JSONObject> objectMap = JsonUtil.getJsonObjects(jsonable.getJson());
    JSONObject mdw = null;
    JSONObject contents = null;
    for (String name : objectMap.keySet()) {
        if ("mdw".equals(name))
            mdw = objectMap.get(name);
        else if (contents == null)
            contents = objectMap.get(name);
    }
    if (contents == null)
        throw new IOException("Cannot find expected contents property");
    else
        objectMap = JsonUtil.getJsonObjects(contents);
    if (mdw != null)
        objectMap.put(".mdw", mdw);

    byte[] buffer = new byte[ZIP_BUFFER_KB * 1024];
    ZipOutputStream zos = null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        zos = new ZipOutputStream(outputStream);
        for (String name : objectMap.keySet()) {
            JSONObject json = objectMap.get(name);
            ZipEntry ze = new ZipEntry(name);
            zos.putNextEntry(ze);
            InputStream inputStream = new ByteArrayInputStream(json.toString(2).getBytes());
            int len;
            while ((len = inputStream.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
        }
    } finally {
        if (zos != null) {
            zos.closeEntry();
            zos.close();
        }
    }
    byte[] bytes = outputStream.toByteArray();
    return new String(Base64.encodeBase64(bytes));
}

From source file:org.apache.sling.maven.slingstart.PreparePackageMojo.java

private void addReadme(ZipOutputStream os) throws IOException {
    ZipEntry ze = new ZipEntry("readme.txt");
    try (InputStream is = getClass().getResourceAsStream("/subsystem-base/readme.txt")) {
        os.putNextEntry(ze);/* w  w w . j  ava  2s. c o m*/
        IOUtils.copy(is, os);
    } finally {
        os.closeEntry();
    }
}