Example usage for java.util.zip ZipOutputStream ZipOutputStream

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

Introduction

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

Prototype

public ZipOutputStream(OutputStream out) 

Source Link

Document

Creates a new ZIP output stream.

Usage

From source file:com.googlecode.dex2jar.v3.Dex2jar.java

public void to(OutputStream os) throws IOException {
    ZipOutputStream zos = new ZipOutputStream(os);
    doTranslate(zos);/*from   w  ww  .  j a  v  a  2  s  .  c  o m*/
    zos.finish();
}

From source file:co.cask.hydrator.transforms.Compressor.java

public static byte[] compressZIP(byte[] input) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(out);
    zip.write(input, 0, input.length);/*from   w w w .jav  a2 s .com*/
    zip.close();
    return out.toByteArray();
}

From source file:de.brendamour.jpasskit.signing.PKFileBasedSigningUtil.java

private byte[] createZippedPassAndReturnAsByteArray(final File tempPassDir) throws PKSigningException {
    ByteArrayOutputStream byteArrayOutputStreamForZippedPass = new ByteArrayOutputStream();
    ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStreamForZippedPass);
    zip(tempPassDir, tempPassDir, zipOutputStream);
    IOUtils.closeQuietly(zipOutputStream);
    return byteArrayOutputStreamForZippedPass.toByteArray();
}

From source file:com.asakusafw.testdriver.compiler.util.DeploymentUtil.java

/**
 * Creates a fat jar from the classpath entries.
 * @param entries the classpath entries/*from  w ww . jav a2s. c  o m*/
 * @param target the target fat jar path
 * @throws IOException if I/O error was occurred while building the target jar
 */
public static void buildFatJar(List<File> entries, File target) throws IOException {
    if (target.getParentFile().isDirectory() == false && target.getParentFile().mkdirs() == false) {
        throw new IOException(
                MessageFormat.format("Failed to copy into {0} (cannot create target directory)", target));
    }
    Set<String> saw = new HashSet<>();
    try (ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target)))) {
        for (File path : entries) {
            if (path.isDirectory()) {
                putEntry(zip, path, null, saw);
            } else {
                mergeEntries(zip, path, saw);
            }
        }
    }
}

From source file:it.geosolutions.tools.compress.file.Compressor.java

/**
 * This function zip the input files./*from  w ww .ja  v  a2 s .co m*/
 * 
 * @param outputDir
 *            The temporary directory where the zip files.
 * @param zipFileBaseName
 *            The name of the zip file.
 * @param files
 *            The array files to zip.
 * @return The zip file or null.
 * @throws IOException
 */
public static File zip(final File outputDir, final String zipFileBaseName, final File[] files)
        throws IOException {

    if (outputDir != null && files != null && zipFileBaseName != null) {

        // //////////////////////////////////////////
        // Create a buffer for reading the files
        // //////////////////////////////////////////
        final File outZipFile = new File(outputDir, zipFileBaseName + ".zip");
        ZipOutputStream out = null;

        try {

            // /////////////////////////////////
            // Create the ZIP output stream
            // /////////////////////////////////

            out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outZipFile)));

            // /////////////////////
            // Compress the files
            // /////////////////////

            for (File file : files) {
                if (file.isDirectory()) {
                    zipDirectory(file, file, out);
                } else {
                    zipFile(file, out);
                }
            }

            out.close();
            out = null;

            return outZipFile;

        } catch (IOException e) {
            if (LOGGER.isErrorEnabled())
                LOGGER.error(e.getLocalizedMessage(), e);
            return null;
        } finally {
            if (out != null)
                out.close();
        }

    } else
        throw new IOException("One or more input parameters are null!");
}

From source file:com.ephesoft.gxt.admin.server.ExportIndexFieldDownloadServlet.java

/**
 * This API is used to process request to export the documents .
 * //from w w w.  ja v a2s.com
 * @param fieldTypeList {@link List<{@link FieldType}>} selected document type list to export.
 * @param resp {@link HttpServletResponse}.
 * @return
 */
private void process(final List<FieldType> fieldTypeList, final HttpServletResponse resp) throws IOException {
    final BatchSchemaService bSService = this.getSingleBeanOfType(BatchSchemaService.class);
    final String exportSerDirPath = bSService.getBatchExportFolderLocation();

    final String zipFileName = fieldTypeList.get(0).getDocType().getName() + "_" + "FieldTypes";

    final String copiedParentFolderPath = exportSerDirPath + File.separator + zipFileName;

    final File copiedFd = createDirectory(copiedParentFolderPath);

    processExportFieldTypes(fieldTypeList, bSService, resp, zipFileName);
    resp.setContentType("application/x-zip\r\n");
    resp.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + ZIP_EXT + "\"\r\n");
    ServletOutputStream out = null;
    ZipOutputStream zout = null;
    try {
        out = resp.getOutputStream();
        zout = new ZipOutputStream(out);
        FileUtils.zipDirectory(copiedParentFolderPath, zout, zipFileName);
        resp.setStatus(HttpServletResponse.SC_OK);
    } catch (final IOException e) {
        // Unable to create the temporary export file(s)/folder(s)
        log.error("Error occurred while creating the zip file." + e, e);
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to export.Please try again.");
    } finally {
        // clean up code
        if (zout != null) {
            zout.close();
        }
        if (out != null) {
            out.flush();
        }
        FileUtils.deleteDirectoryAndContentsRecursive(copiedFd);
    }
}

From source file:pl.betoncraft.betonquest.editor.model.PackageSet.java

/**
 * Saves the package to a .zip file.//from  w w  w . j  ava 2s  .  com
 * 
 * @param zipFile
 */
public void saveToZip(File zip) {
    try {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip));
        for (QuestPackage pack : packages) {
            String prefix = pack.getName().get().replace("-", File.separator) + File.separator;
            // save main.yml file
            ZipEntry main = new ZipEntry(prefix + "main.yml");
            out.putNextEntry(main);
            pack.printMainYAML(out);
            out.closeEntry();
            // save conversation files
            for (Conversation conv : pack.getConversations()) {
                ZipEntry conversation = new ZipEntry(
                        prefix + "conversations" + File.separator + conv.getId().get() + ".yml");
                out.putNextEntry(conversation);
                pack.printConversationYaml(out, conv);
                out.closeEntry();
            }
            // save events.yml file
            ZipEntry events = new ZipEntry(prefix + "events.yml");
            out.putNextEntry(events);
            pack.printEventsYaml(out);
            out.closeEntry();
            // save conditions.yml file
            ZipEntry conditions = new ZipEntry(prefix + "conditions.yml");
            out.putNextEntry(conditions);
            pack.printConditionsYaml(out);
            out.closeEntry();
            // save objectives.yml file
            ZipEntry objectives = new ZipEntry(prefix + "objectives.yml");
            out.putNextEntry(objectives);
            pack.printObjectivesYaml(out);
            out.closeEntry();
            // save items.yml file
            ZipEntry items = new ZipEntry(prefix + "items.yml");
            out.putNextEntry(items);
            pack.printItemsYaml(out);
            out.closeEntry();
            // save journal.yml file
            ZipEntry journal = new ZipEntry(prefix + "journal.yml");
            out.putNextEntry(journal);
            pack.printJournalYaml(out);
            out.closeEntry();
        }
        // done
        out.close();
    } catch (Exception e) {
        ExceptionController.display(e);
    }
}

From source file:S3DataManagerTest.java

@Test
public void testZipSourceOneDirEmpty() throws Exception {
    clearSourceDirectory();/*from   w  ww.j a  v  a  2  s  . co m*/
    String buildSpecName = "Buildspec.yml";
    File buildSpec = new File("/tmp/source/" + buildSpecName);
    String buildSpecContents = "yo\n";
    FileUtils.write(buildSpec, buildSpecContents);
    File sourceDir = new File("/tmp/source/src");
    sourceDir.mkdir();

    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 == 1);
    assertTrue(unzipFolder.list()[0].equals(buildSpecName));
    File extractedBuildSpec = new File(unzipFolder.getPath() + "/" + buildSpecName);
    assertTrue(FileUtils.readFileToString(extractedBuildSpec).equals(buildSpecContents));
}

From source file:net.solarnetwork.node.backup.DefaultBackupManager.java

@Override
public void exportBackupArchive(String backupKey, OutputStream out) throws IOException {
    final BackupService service = activeBackupService();
    if (service == null) {
        return;// w  w w .  j a v a2  s .  c o m
    }

    final Backup backup = service.backupForKey(backupKey);
    if (backup == null) {
        return;
    }

    // create the zip archive for the backup files
    ZipOutputStream zos = new ZipOutputStream(out);
    try {
        BackupResourceIterable resources = service.getBackupResources(backup);
        for (BackupResource r : resources) {
            zos.putNextEntry(new ZipEntry(r.getBackupPath()));
            FileCopyUtils.copy(r.getInputStream(), new FilterOutputStream(zos) {

                @Override
                public void close() throws IOException {
                    // FileCopyUtils closed the stream, which we don't want here
                }

            });
        }
        resources.close();
    } finally {
        zos.flush();
        zos.finish();
        zos.close();
    }
}

From source file:br.org.indt.ndg.server.client.TemporaryOpenRosaBussinessDelegate.java

public String exportZippedResultsForUser(String deviceId) throws FileNotFoundException {
    String resultsDir = deviceId + File.separator;
    String zipFilename = deviceId + ".zip";
    new File(resultsDir).mkdir();
    saveResultsToFilesForDeviceId(deviceId, resultsDir);

    ZipOutputStream zipOutputStream = null;
    try {//from   w ww  .  j a va2  s .  c o m
        zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilename));
        File zipDir = new File(resultsDir);
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[4096];
        int bytesIn = 0;

        for (int i = 0; i < dirList.length; i++) {
            FileInputStream fis = null;
            try {
                File f = new File(zipDir, dirList[i]);
                fis = new FileInputStream(f);
                ZipEntry anEntry = new ZipEntry(f.getPath());
                zipOutputStream.putNextEntry(anEntry);
                while ((bytesIn = fis.read(readBuffer)) != -1) {
                    zipOutputStream.write(readBuffer, 0, bytesIn);
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    } finally {
        if (zipOutputStream != null) {
            try {
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    removeDir(resultsDir);
    return zipFilename;
}