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

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

Introduction

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

Prototype

public ZipArchiveOutputStream(File file) throws IOException 

Source Link

Document

Creates a new ZIP OutputStream writing to a File.

Usage

From source file:com.haulmont.cuba.core.sys.logging.LogArchiver.java

public static void writeArchivedLogToStream(File logFile, OutputStream outputStream) throws IOException {
    if (!logFile.exists()) {
        throw new FileNotFoundException();
    }/*  w w w  .j a v a  2 s . co m*/

    File tempFile = File.createTempFile(FilenameUtils.getBaseName(logFile.getName()) + "_log_", ".zip");

    ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(tempFile);
    zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED);
    zipOutputStream.setEncoding(ZIP_ENCODING);

    ArchiveEntry archiveEntry = newArchive(logFile);
    zipOutputStream.putArchiveEntry(archiveEntry);

    FileInputStream logFileInput = new FileInputStream(logFile);
    IOUtils.copyLarge(logFileInput, zipOutputStream);

    logFileInput.close();

    zipOutputStream.closeArchiveEntry();
    zipOutputStream.close();

    FileInputStream tempFileInput = new FileInputStream(tempFile);
    IOUtils.copyLarge(tempFileInput, outputStream);

    tempFileInput.close();

    FileUtils.deleteQuietly(tempFile);
}

From source file:jetbrains.exodus.util.CompressBackupUtil.java

@NotNull
public static File backup(@NotNull final Backupable target, @NotNull final File backupRoot,
        @Nullable final String backupNamePrefix, final boolean zip) throws Exception {
    if (!backupRoot.exists() && !backupRoot.mkdirs()) {
        throw new IOException("Failed to create " + backupRoot.getAbsolutePath());
    }/*from  w w  w  .j av  a  2  s.  c  om*/
    final File backupFile;
    final BackupStrategy strategy = target.getBackupStrategy();
    strategy.beforeBackup();
    try {
        final ArchiveOutputStream archive;
        if (zip) {
            final String fileName = getTimeStampedZipFileName();
            backupFile = new File(backupRoot,
                    backupNamePrefix == null ? fileName : backupNamePrefix + fileName);
            final ZipArchiveOutputStream zipArchive = new ZipArchiveOutputStream(
                    new BufferedOutputStream(new FileOutputStream(backupFile)));
            zipArchive.setLevel(Deflater.BEST_COMPRESSION);
            archive = zipArchive;
        } else {
            final String fileName = getTimeStampedTarGzFileName();
            backupFile = new File(backupRoot,
                    backupNamePrefix == null ? fileName : backupNamePrefix + fileName);
            archive = new TarArchiveOutputStream(
                    new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(backupFile))));
        }
        try (ArchiveOutputStream aos = archive) {
            for (final BackupStrategy.FileDescriptor fd : strategy.listFiles()) {
                if (strategy.isInterrupted()) {
                    break;
                }
                final File file = fd.getFile();
                if (file.isFile()) {
                    final long fileSize = Math.min(fd.getFileSize(), strategy.acceptFile(file));
                    if (fileSize > 0L) {
                        archiveFile(aos, fd.getPath(), file, fileSize);
                    }
                }
            }
        }
        if (strategy.isInterrupted()) {
            logger.info("Backup interrupted, deleting \"" + backupFile.getName() + "\"...");
            IOUtil.deleteFile(backupFile);
        } else {
            logger.info("Backup file \"" + backupFile.getName() + "\" created.");
        }
    } catch (Throwable t) {
        strategy.onError(t);
        throw ExodusException.toExodusException(t, "Backup failed");
    } finally {
        strategy.afterBackup();
    }
    return backupFile;
}

From source file:com.streamsets.datacollector.restapi.ZipEdgeArchiveBuilder.java

@Override
public void finish() throws IOException {
    try (ZipArchiveOutputStream zipArchiveOutput = new ZipArchiveOutputStream(outputStream);
            ZipArchiveInputStream zipArchiveInput = new ZipArchiveInputStream(
                    new FileInputStream(edgeArchive))) {
        ZipArchiveEntry entry = zipArchiveInput.getNextZipEntry();

        while (entry != null) {
            zipArchiveOutput.putArchiveEntry(entry);
            IOUtils.copy(zipArchiveInput, zipArchiveOutput);
            zipArchiveOutput.closeArchiveEntry();
            entry = zipArchiveInput.getNextZipEntry();
        }/* w ww  .  ja v  a 2  s .c  o  m*/

        for (PipelineConfigurationJson pipelineConfiguration : pipelineConfigurationList) {
            addArchiveEntry(zipArchiveOutput, pipelineConfiguration, pipelineConfiguration.getPipelineId(),
                    PIPELINE_JSON_FILE);
            addArchiveEntry(zipArchiveOutput, pipelineConfiguration.getInfo(),
                    pipelineConfiguration.getPipelineId(), PIPELINE_INFO_FILE);
        }

        zipArchiveOutput.finish();
    }
}

From source file:hudson.util.io.ZipArchiver.java

ZipArchiver(OutputStream out) {
    zip = new ZipArchiveOutputStream(out);
    zip.setEncoding(System.getProperty("file.encoding"));
}

From source file:fr.gael.ccsds.sip.archive.ZipArchiveManager.java

/**
 * Produces Zip compressed archive./*from   w w  w  . j  av a  2  s.co m*/
 */
@Override
public File copy(final File src, final File zip_file, final String dst) throws Exception {
    if (zip_file.exists()) {
        final FileInputStream fis = new FileInputStream(zip_file);
        final ZipArchiveInputStream zis = new ZipArchiveInputStream(fis);

        final File tempFile = File.createTempFile("updateZip", "zip");
        final FileOutputStream fos = new FileOutputStream(tempFile);
        final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);

        // copy the existing entries
        ZipArchiveEntry nextEntry;
        while ((nextEntry = zis.getNextZipEntry()) != null) {
            zos.putArchiveEntry(nextEntry);
            IOUtils.copy(zis, zos);
            zos.closeArchiveEntry();
        }

        // create the new entry
        final ZipArchiveEntry entry = new ZipArchiveEntry(src, dst);
        entry.setSize(src.length());
        zos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, zos);
        sfis.close();
        zos.closeArchiveEntry();

        zos.finish();
        zis.close();
        fis.close();
        zos.close();

        // Rename the new file over the old
        boolean status = zip_file.delete();
        File saved_tempFile = tempFile;
        status = tempFile.renameTo(zip_file);

        // Copy the new file over the old if the renaming failed
        if (!status) {
            final FileInputStream tfis = new FileInputStream(saved_tempFile);
            final FileOutputStream tfos = new FileOutputStream(zip_file);

            final byte[] buf = new byte[1024];
            int i = 0;

            while ((i = tfis.read(buf)) != -1) {
                tfos.write(buf, 0, i);
            }

            tfis.close();
            tfos.close();

            saved_tempFile.delete();
        }

        return zip_file;

    } else {
        final FileOutputStream fos = new FileOutputStream(zip_file);
        final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);

        final ZipArchiveEntry entry = new ZipArchiveEntry(src, dst);
        entry.setSize(src.length());
        zos.putArchiveEntry(entry);

        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, zos);
        sfis.close();

        zos.closeArchiveEntry();

        zos.finish();
        zos.close();
        fos.close();
    }
    return zip_file;
}

From source file:com.oculusinfo.binning.io.impl.ZipResourcePyramidSourceTest.java

@Test
public void test() {

    ZipArchiveOutputStream zos = null;//  ww w.j a v a2  s . co  m
    File archive;
    String filename = "";
    try {
        archive = File.createTempFile("test.", ".zip", null);
        archive.deleteOnExit();
        filename = archive.getAbsolutePath();
        zos = new ZipArchiveOutputStream(archive);

        for (int z = 0; z < 3; z++) {
            for (int x = 0; x < Math.pow(2, z); x++) {
                for (int y = 0; y < Math.pow(2, z); y++) {
                    ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(),
                            "test/tiles/" + z + "/" + x + "/" + y + ".dummy");
                    zos.putArchiveEntry(entry);
                }
            }
        }

        ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/metadata.json");
        zos.putArchiveEntry(entry);

        zos.closeArchiveEntry();
        zos.close();
        zos = null;
    } catch (IOException e) {
        fail(e.getMessage());
    }

    try {
        ZipResourcePyramidSource src = new ZipResourcePyramidSource(filename, "dummy");

        TileIndex tileDef = new TileIndex(0, 0, 0, 1, 1);
        InputStream is = src.getSourceTileStream("test", tileDef);
        Assert.assertTrue(is != null);

        tileDef = new TileIndex(2, 3, 3, 1, 1);
        is = src.getSourceTileStream("test", tileDef);
        Assert.assertTrue(is != null);

        is = src.getSourceMetaDataStream("test");
        Assert.assertTrue(is != null);

    } catch (IOException e) {
        fail(e.getMessage());
    }

}

From source file:com.oculusinfo.binning.io.impl.ZipResourcePyramidStreamSourceTest.java

@Test
public void test() {

    ZipArchiveOutputStream zos = null;// ww w.j  av  a2s  . com
    File archive;
    String filename = "";
    try {
        archive = File.createTempFile("test.", ".zip", null);
        archive.deleteOnExit();
        filename = archive.getAbsolutePath();
        zos = new ZipArchiveOutputStream(archive);

        for (int z = 0; z < 3; z++) {
            for (int x = 0; x < Math.pow(2, z); x++) {
                for (int y = 0; y < Math.pow(2, z); y++) {
                    ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(),
                            "test/tiles/" + z + "/" + x + "/" + y + ".dummy");
                    zos.putArchiveEntry(entry);
                }
            }
        }

        ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/metadata.json");
        zos.putArchiveEntry(entry);

        zos.closeArchiveEntry();
        zos.close();
        zos = null;
    } catch (IOException e) {
        fail(e.getMessage());
    }

    try {
        ZipResourcePyramidStreamSource src = new ZipResourcePyramidStreamSource(filename, "dummy");

        TileIndex tileDef = new TileIndex(0, 0, 0, 1, 1);
        InputStream is = src.getTileStream("test", tileDef);
        Assert.assertTrue(is != null);

        tileDef = new TileIndex(2, 3, 3, 1, 1);
        is = src.getTileStream("test", tileDef);
        Assert.assertTrue(is != null);

        is = src.getMetaDataStream("test");
        Assert.assertTrue(is != null);

    } catch (IOException e) {
        fail(e.getMessage());
    }

}

From source file:com.google.jenkins.plugins.persistentmaster.volume.zip.ZipCreator.java

ZipCreator(Path zip) throws IOException {
    zipPath = Preconditions.checkNotNull(zip);
    Preconditions.checkArgument(!Files.exists(zipPath), "zip file exists");
    logger.finer("Creating zip volume for path: " + zipPath);
    zipStream = new ZipArchiveOutputStream(Files.newOutputStream(zipPath, StandardOpenOption.CREATE_NEW));
    // unfortunately there is no typesafe way of doing this
    zipStream.setEncoding(UTF_8);/*from w ww. j a  va 2 s. c o m*/
    zipStream.setUseZip64(Zip64Mode.AsNeeded);
}

From source file:com.sic.bb.jenkins.plugins.sicci_for_xcode.util.ExtendedFile.java

public void zip(File zipFile) throws IOException, InterruptedException {
    FileOutputStream zipFileOutputStream = null;
    ZipArchiveOutputStream zipStream = null;

    try {/*from ww w .j av  a2 s .c  o  m*/
        zipFileOutputStream = new FileOutputStream(zipFile);
        zipStream = new ZipArchiveOutputStream(zipFileOutputStream);
        zipDirectory(this, null, zipStream);
    } finally {
        IOUtils.closeQuietly(zipStream);
        IOUtils.closeQuietly(zipFileOutputStream);
    }
}

From source file:com.silverpeas.util.ZipManager.java

/**
 * Compress a file into a zip file.// w ww. j  ava 2s  . com
 *
 * @param filePath
 * @param zipFilePath
 * @return
 * @throws IOException
 */
public static long compressFile(String filePath, String zipFilePath) throws IOException {
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new FileOutputStream(zipFilePath));
    InputStream in = new FileInputStream(filePath);
    try {
        // cration du flux zip
        zos = new ZipArchiveOutputStream(new FileOutputStream(zipFilePath));
        zos.setFallbackToUTF8(true);
        zos.setCreateUnicodeExtraFields(NOT_ENCODEABLE);
        zos.setEncoding(CharEncoding.UTF_8);
        String entryName = FilenameUtils.getName(filePath);
        entryName = entryName.replace(File.separatorChar, '/');
        zos.putArchiveEntry(new ZipArchiveEntry(entryName));
        IOUtils.copy(in, zos);
        zos.closeArchiveEntry();
        return new File(zipFilePath).length();
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(zos);
    }
}