Example usage for java.util.zip Deflater DEFAULT_COMPRESSION

List of usage examples for java.util.zip Deflater DEFAULT_COMPRESSION

Introduction

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

Prototype

int DEFAULT_COMPRESSION

To view the source code for java.util.zip Deflater DEFAULT_COMPRESSION.

Click Source Link

Document

Default compression level.

Usage

From source file:org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveWriter.java

public NuxeoArchiveWriter(File destination) throws IOException {
    this(new BufferedOutputStream(new FileOutputStream(destination)), Deflater.DEFAULT_COMPRESSION);
}

From source file:org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveWriter.java

public NuxeoArchiveWriter(OutputStream out) throws IOException {
    this(new ZipOutputStream(out), Deflater.DEFAULT_COMPRESSION);
}

From source file:org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveWriter.java

public NuxeoArchiveWriter(ZipOutputStream out) throws IOException {
    this(out, Deflater.DEFAULT_COMPRESSION);
}

From source file:org.nuxeo.ecm.server.info.service.impl.ServerInfoCollectorImpl.java

@Override
public File collectInfoAsZip() throws Exception {

    // TODO: let the caller delete the file when done with it
    File tmpFile = File.createTempFile("NXserverInfo-", ".zip");
    tmpFile.deleteOnExit();//from   w ww  .  ja va2s. c o  m

    FileOutputStream fos = new FileOutputStream(tmpFile);
    ZipOutputStream out = new ZipOutputStream(fos);
    out.setLevel(Deflater.DEFAULT_COMPRESSION);
    try {
        // System
        // TODO: externalize logic from Seam component to a service
        SystemInfoManager sysInfoManager = new SystemInfoManager();
        String sysInfo = sysInfoManager.getHostInfo();
        addZipEntry(out, "system.info", sysInfo);

        // Nuxeo distrib
        SimplifiedServerInfo serverInfo = RuntimeInstrospection.getInfo();
        String distribInfo = getDistribInfo(serverInfo);
        addZipEntry(out, "distrib.info", distribInfo);

        // Installed marketplace packages
        ConfigurationGenerator cg = new ConfigurationGenerator();
        // String installedPackages = getInstalledPackages(cg.getEnv());
        // addZipEntry(out, "packages.info", installedPackages);

        // Nuxeo configuration (nuxeo.conf)
        File configFile = cg.getNuxeoConf();
        addZipEntry(out, "nuxeo.conf", configFile);

        // Server logs (server.log)
        File logFile = new File(cg.getLogDir(), "server.log");
        addZipEntry(out, "server.log", logFile);

        return tmpFile;
    } finally {
        try {
            out.close();
            fos.close();
        } catch (IOException e) {
            log.error("Error while closing output streams");
        }
    }
}

From source file:org.openflexo.toolbox.ZipUtils.java

/**
 * This method makes a zip file on file <code>zipOutput</code>out of the given <code>fileToZip</code>
 * /*from  w  w  w.j a va 2  s. c o m*/
 * @param zipOutput
 *            - the output where to write the zip
 * @param fileToZip
 *            the file to zip (wheter it is a file or a directory)
 * @throws IOException
 */
public static void makeZip(File zipOutput, File fileToZip, IProgress progress, FileFilter filter)
        throws IOException {
    makeZip(zipOutput, fileToZip, progress, filter, Deflater.DEFAULT_COMPRESSION);
}

From source file:org.opensha.commons.util.FileUtils.java

/**
 * This creates a zip file with the given name from a list of file names.
 * //from   w  w w.j a  va  2 s  . c  o m
 * @param zipFile
 * @param files
 * @throws IOException
 */
public static void createZipFile(String zipFile, String dir, Collection<String> fileNames) throws IOException {

    byte[] buffer = new byte[18024];

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

    if (dir.length() > 0 && !dir.endsWith(File.separator))
        dir += File.separator;

    // Set the compression ratio
    out.setLevel(Deflater.DEFAULT_COMPRESSION);

    // iterate through the array of files, adding each to the zip file
    for (String file : fileNames) {
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(file));

        File f = new File(dir + file);
        if (f.isDirectory())
            continue;

        // Associate a file input stream for the current file
        FileInputStream in = new FileInputStream(f);

        // Transfer bytes from the current file to the ZIP file
        //out.write(buffer, 0, in.read(buffer));

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

        // Close the current entry
        out.closeEntry();

        // Close the current file input stream
        in.close();

    }
    // Close the ZipOutPutStream
    out.close();
}

From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java

public boolean processRowFile(Job parentJob, Result result, String realZipfilename, String realWildcard,
        String realWildcardExclude, String realSourceDirectoryOrFile, String realMovetodirectory,
        boolean createparentfolder) {
    boolean Fileexists = false;
    File tempFile = null;//from  w w  w  .  j  av  a2  s . co m
    File fileZip = null;
    boolean resultat = false;
    boolean renameOk = false;
    boolean orginExist = false;

    // Check if target file/folder exists!
    FileObject originFile = null;
    ZipInputStream zin = null;
    byte[] buffer = null;
    OutputStream dest = null;
    BufferedOutputStream buff = null;
    ZipOutputStream out = null;
    ZipEntry entry = null;
    String localSourceFilename = realSourceDirectoryOrFile;

    try {
        originFile = KettleVFS.getFileObject(realSourceDirectoryOrFile, this);
        localSourceFilename = KettleVFS.getFilename(originFile);
        orginExist = originFile.exists();
    } catch (Exception e) {
        // Ignore errors
    } finally {
        if (originFile != null) {
            try {
                originFile.close();
            } catch (IOException ex) {
                logError("Error closing file '" + originFile.toString() + "'", ex);
            }
        }
    }

    String localrealZipfilename = realZipfilename;
    if (realZipfilename != null && orginExist) {

        FileObject fileObject = null;
        try {
            fileObject = KettleVFS.getFileObject(localrealZipfilename, this);
            localrealZipfilename = KettleVFS.getFilename(fileObject);
            // Check if Zip File exists
            if (fileObject.exists()) {
                Fileexists = true;
                if (log.isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileExists1.Label")
                            + localrealZipfilename
                            + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileExists2.Label"));
                }
            }
            // Let's see if we need to create parent folder of destination zip filename
            if (createparentfolder) {
                createParentFolder(localrealZipfilename);
            }

            // Let's start the process now
            if (ifZipFileExists == 3 && Fileexists) {
                // the zip file exists and user want to Fail
                resultat = false;
            } else if (ifZipFileExists == 2 && Fileexists) {
                // the zip file exists and user want to do nothing
                if (addFileToResult) {
                    // Add file to result files name
                    ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject,
                            parentJob.getJobname(), toString());
                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                }
                resultat = true;
            } else if (afterZip == 2 && realMovetodirectory == null) {
                // After Zip, Move files..User must give a destination Folder
                resultat = false;
                logError(
                        BaseMessages.getString(PKG, "JobZipFiles.AfterZip_No_DestinationFolder_Defined.Label"));
            } else {
                // After Zip, Move files..User must give a destination Folder

                // Let's see if we deal with file or folder
                FileObject[] fileList = null;

                FileObject sourceFileOrFolder = KettleVFS.getFileObject(localSourceFilename);
                boolean isSourceDirectory = sourceFileOrFolder.getType().equals(FileType.FOLDER);
                final Pattern pattern;
                final Pattern patternexclude;

                if (isSourceDirectory) {
                    // Let's prepare the pattern matcher for performance reasons.
                    // We only do this if the target is a folder !
                    //
                    if (!Const.isEmpty(realWildcard)) {
                        pattern = Pattern.compile(realWildcard);
                    } else {
                        pattern = null;
                    }
                    if (!Const.isEmpty(realWildcardExclude)) {
                        patternexclude = Pattern.compile(realWildcardExclude);
                    } else {
                        patternexclude = null;
                    }

                    // Target is a directory
                    // Get all the files in the directory...
                    //
                    if (includingSubFolders) {
                        fileList = sourceFileOrFolder.findFiles(new FileSelector() {

                            public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception {
                                return true;
                            }

                            public boolean includeFile(FileSelectInfo fileInfo) throws Exception {
                                boolean include;

                                // Only include files in the sub-folders...
                                // When we include sub-folders we match the whole filename, not just the base-name
                                //
                                if (fileInfo.getFile().getType().equals(FileType.FILE)) {
                                    include = true;
                                    if (pattern != null) {
                                        String name = fileInfo.getFile().getName().getPath();
                                        include = pattern.matcher(name).matches();
                                    }
                                    if (include && patternexclude != null) {
                                        String name = fileInfo.getFile().getName().getPath();
                                        include = !pattern.matcher(name).matches();
                                    }
                                } else {
                                    include = false;
                                }
                                return include;
                            }
                        });
                    } else {
                        fileList = sourceFileOrFolder.getChildren();
                    }
                } else {
                    pattern = null;
                    patternexclude = null;

                    // Target is a file
                    fileList = new FileObject[] { sourceFileOrFolder };
                }

                if (fileList.length == 0) {
                    resultat = false;
                    logError(BaseMessages.getString(PKG, "JobZipFiles.Log.FolderIsEmpty", localSourceFilename));
                } else if (!checkContainsFile(localSourceFilename, fileList, isSourceDirectory)) {
                    resultat = false;
                    logError(BaseMessages.getString(PKG, "JobZipFiles.Log.NoFilesInFolder",
                            localSourceFilename));
                } else {
                    if (ifZipFileExists == 0 && Fileexists) {
                        // the zip file exists and user want to create new one with unique name
                        // Format Date

                        // do we have already a .zip at the end?
                        if (localrealZipfilename.toLowerCase().endsWith(".zip")) {
                            // strip this off
                            localrealZipfilename = localrealZipfilename.substring(0,
                                    localrealZipfilename.length() - 4);
                        }

                        localrealZipfilename += "_" + StringUtil.getFormattedDateTimeNow(true) + ".zip";
                        if (log.isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileNameChange1.Label")
                                    + localrealZipfilename
                                    + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileNameChange1.Label"));
                        }
                    } else if (ifZipFileExists == 1 && Fileexists) {
                        // the zip file exists and user want to append
                        // get a temp file
                        fileZip = getFile(localrealZipfilename);
                        tempFile = File.createTempFile(fileZip.getName(), null);

                        // delete it, otherwise we cannot rename existing zip to it.
                        tempFile.delete();

                        renameOk = fileZip.renameTo(tempFile);

                        if (!renameOk) {
                            logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp1.Label")
                                    + fileZip.getAbsolutePath()
                                    + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp2.Label")
                                    + tempFile.getAbsolutePath()
                                    + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp3.Label"));
                        }
                        if (log.isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend1.Label")
                                    + localrealZipfilename
                                    + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend2.Label"));
                        }
                    }

                    if (log.isDetailed()) {
                        logDetailed(
                                BaseMessages.getString(PKG, "JobZipFiles.Files_Found1.Label") + fileList.length
                                        + BaseMessages.getString(PKG, "JobZipFiles.Files_Found2.Label")
                                        + localSourceFilename
                                        + BaseMessages.getString(PKG, "JobZipFiles.Files_Found3.Label"));
                    }

                    // Prepare Zip File
                    buffer = new byte[18024];
                    dest = KettleVFS.getOutputStream(localrealZipfilename, false);
                    buff = new BufferedOutputStream(dest);
                    out = new ZipOutputStream(buff);

                    HashSet<String> fileSet = new HashSet<String>();

                    if (renameOk) {
                        // User want to append files to existing Zip file
                        // The idea is to rename the existing zip file to a temporary file
                        // and then adds all entries in the existing zip along with the new files,
                        // excluding the zip entries that have the same name as one of the new files.

                        zin = new ZipInputStream(new FileInputStream(tempFile));
                        entry = zin.getNextEntry();

                        while (entry != null) {
                            String name = entry.getName();

                            if (!fileSet.contains(name)) {

                                // Add ZIP entry to output stream.
                                out.putNextEntry(new ZipEntry(name));
                                // Transfer bytes from the ZIP file to the output file
                                int len;
                                while ((len = zin.read(buffer)) > 0) {
                                    out.write(buffer, 0, len);
                                }

                                fileSet.add(name);
                            }
                            entry = zin.getNextEntry();
                        }
                        // Close the streams
                        zin.close();
                    }

                    // Set the method
                    out.setMethod(ZipOutputStream.DEFLATED);
                    // Set the compression level
                    if (compressionRate == 0) {
                        out.setLevel(Deflater.NO_COMPRESSION);
                    } else if (compressionRate == 1) {
                        out.setLevel(Deflater.DEFAULT_COMPRESSION);
                    }
                    if (compressionRate == 2) {
                        out.setLevel(Deflater.BEST_COMPRESSION);
                    }
                    if (compressionRate == 3) {
                        out.setLevel(Deflater.BEST_SPEED);
                    }
                    // Specify Zipped files (After that we will move,delete them...)
                    FileObject[] zippedFiles = new FileObject[fileList.length];
                    int fileNum = 0;

                    // Get the files in the list...
                    for (int i = 0; i < fileList.length && !parentJob.isStopped(); i++) {
                        boolean getIt = true;
                        boolean getItexclude = false;

                        // First see if the file matches the regular expression!
                        // ..only if target is a folder !
                        if (isSourceDirectory) {
                            // If we include sub-folders, we match on the whole name, not just the basename
                            //
                            String filename;
                            if (includingSubFolders) {
                                filename = fileList[i].getName().getPath();
                            } else {
                                filename = fileList[i].getName().getBaseName();
                            }
                            if (pattern != null) {
                                // Matches the base name of the file (backward compatible!)
                                //
                                Matcher matcher = pattern.matcher(filename);
                                getIt = matcher.matches();
                            }

                            if (patternexclude != null) {
                                Matcher matcherexclude = patternexclude.matcher(filename);
                                getItexclude = matcherexclude.matches();
                            }
                        }

                        // Get processing File
                        String targetFilename = KettleVFS.getFilename(fileList[i]);
                        if (sourceFileOrFolder.getType().equals(FileType.FILE)) {
                            targetFilename = localSourceFilename;
                        }

                        FileObject file = KettleVFS.getFileObject(targetFilename);
                        boolean isTargetDirectory = file.exists() && file.getType().equals(FileType.FOLDER);

                        if (getIt && !getItexclude && !isTargetDirectory && !fileSet.contains(targetFilename)) {
                            // We can add the file to the Zip Archive
                            if (log.isDebug()) {
                                logDebug(BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip1.Label")
                                        + fileList[i]
                                        + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip2.Label")
                                        + localSourceFilename
                                        + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip3.Label"));
                            }

                            // Associate a file input stream for the current file
                            InputStream in = KettleVFS.getInputStream(file);

                            // Add ZIP entry to output stream.
                            //
                            String relativeName;
                            String fullName = fileList[i].getName().getPath();
                            String basePath = sourceFileOrFolder.getName().getPath();
                            if (isSourceDirectory) {
                                if (fullName.startsWith(basePath)) {
                                    relativeName = fullName.substring(basePath.length() + 1);
                                } else {
                                    relativeName = fullName;
                                }
                            } else if (isFromPrevious) {
                                int depth = determineDepth(environmentSubstitute(storedSourcePathDepth));
                                relativeName = determineZipfilenameForDepth(fullName, depth);
                            } else {
                                relativeName = fileList[i].getName().getBaseName();
                            }
                            out.putNextEntry(new ZipEntry(relativeName));

                            int len;
                            while ((len = in.read(buffer)) > 0) {
                                out.write(buffer, 0, len);
                            }
                            out.flush();
                            out.closeEntry();

                            // Close the current file input stream
                            in.close();

                            // Get Zipped File
                            zippedFiles[fileNum] = fileList[i];
                            fileNum = fileNum + 1;
                        }
                    }
                    // Close the ZipOutPutStream
                    out.close();
                    buff.close();
                    dest.close();

                    if (log.isBasic()) {
                        logBasic(BaseMessages.getString(PKG, "JobZipFiles.Log.TotalZippedFiles",
                                "" + zippedFiles.length));
                    }
                    // Delete Temp File
                    if (tempFile != null) {
                        tempFile.delete();
                    }

                    // -----Get the list of Zipped Files and Move or Delete Them
                    if (afterZip == 1 || afterZip == 2) {
                        // iterate through the array of Zipped files
                        for (int i = 0; i < zippedFiles.length; i++) {
                            if (zippedFiles[i] != null) {
                                // Delete, Move File
                                FileObject fileObjectd = zippedFiles[i];
                                if (!isSourceDirectory) {
                                    fileObjectd = KettleVFS.getFileObject(localSourceFilename);
                                }

                                // Here we can move, delete files
                                if (afterZip == 1) {
                                    // Delete File
                                    boolean deleted = fileObjectd.delete();
                                    if (!deleted) {
                                        resultat = false;
                                        logError(BaseMessages.getString(PKG,
                                                "JobZipFiles.Cant_Delete_File1.Label") + localSourceFilename
                                                + Const.FILE_SEPARATOR + zippedFiles[i] + BaseMessages
                                                        .getString(PKG, "JobZipFiles.Cant_Delete_File2.Label"));

                                    }
                                    // File deleted
                                    if (log.isDebug()) {
                                        logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Deleted1.Label")
                                                + localSourceFilename + Const.FILE_SEPARATOR + zippedFiles[i]
                                                + BaseMessages.getString(PKG,
                                                        "JobZipFiles.File_Deleted2.Label"));
                                    }
                                } else if (afterZip == 2) {
                                    // Move File
                                    FileObject fileObjectm = null;
                                    try {
                                        fileObjectm = KettleVFS.getFileObject(realMovetodirectory
                                                + Const.FILE_SEPARATOR + fileObjectd.getName().getBaseName());
                                        fileObjectd.moveTo(fileObjectm);
                                    } catch (IOException e) {
                                        logError(
                                                BaseMessages.getString(PKG, "JobZipFiles.Cant_Move_File1.Label")
                                                        + zippedFiles[i]
                                                        + BaseMessages.getString(PKG,
                                                                "JobZipFiles.Cant_Move_File2.Label")
                                                        + e.getMessage());
                                        resultat = false;
                                    } finally {
                                        try {
                                            if (fileObjectm != null) {
                                                fileObjectm.close();
                                            }
                                        } catch (Exception e) {
                                            if (fileObjectm != null) {
                                                logError("Error closing file '" + fileObjectm.toString() + "'",
                                                        e);
                                            }
                                        }
                                    }
                                    // File moved
                                    if (log.isDebug()) {
                                        logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Moved1.Label")
                                                + zippedFiles[i]
                                                + BaseMessages.getString(PKG, "JobZipFiles.File_Moved2.Label"));
                                    }
                                }
                            }
                        }
                    }

                    if (addFileToResult) {
                        // Add file to result files name
                        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject,
                                parentJob.getJobname(), toString());
                        result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                    }

                    resultat = true;
                }
            }
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile1.Label") + localrealZipfilename
                    + BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile2.Label"), e);
            resultat = false;
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                    fileObject = null;
                } catch (IOException ex) {
                    logError("Error closing file '" + fileObject.toString() + "'", ex);
                }
            }

            try {
                if (out != null) {
                    out.close();
                }
                if (buff != null) {
                    buff.close();
                }
                if (dest != null) {
                    dest.close();
                }
                if (zin != null) {
                    zin.close();
                }
                if (entry != null) {
                    entry = null;
                }

            } catch (IOException ex) {
                logError("Error closing zip file entry for file '" + originFile.toString() + "'", ex);
            }
        }
    } else {
        resultat = true;
        if (localrealZipfilename == null) {
            logError(BaseMessages.getString(PKG, "JobZipFiles.No_ZipFile_Defined.Label"));
        }
        if (!orginExist) {
            logError(BaseMessages.getString(PKG, "JobZipFiles.No_FolderCible_Defined.Label",
                    localSourceFilename));
        }
    }
    // return a verifier
    return resultat;
}

From source file:org.pentaho.hadoop.shim.common.format.avro.PentahoAvroOutputFormat.java

@Override
public void setCompression(COMPRESSION compression) {
    switch (compression) {
    case SNAPPY:/* w w w  . ja  v a  2s.  c  o m*/
        codecFactory = CodecFactory.snappyCodec();
        break;
    case DEFLATE:
        codecFactory = CodecFactory.deflateCodec(Deflater.DEFAULT_COMPRESSION);
        break;
    default:
        codecFactory = CodecFactory.nullCodec();
        break;
    }
}

From source file:org.phpmaven.phar.PharJavaPackager.java

private void packFile(final ByteArrayOutputStream fileEntriesBaos,
        final ByteArrayOutputStream compressedFilesBaos, final File fileToPack, String filePath)
        throws IOException {
    if (DEBUG) {/*from   ww  w  .j a v  a  2 s .  c  o  m*/
        System.out.println("Packing file " + fileToPack + " with " + fileToPack.length() + " bytes.");
    }

    final byte[] fileBytes = filePath.getBytes("UTF-8");
    writeIntLE(fileEntriesBaos, fileBytes.length);
    fileEntriesBaos.write(fileBytes);
    // TODO Complain with files larger than 4 bytes file length
    writeIntLE(fileEntriesBaos, (int) fileToPack.length());
    writeIntLE(fileEntriesBaos, (int) (fileToPack.lastModified() / 1000));

    final byte[] uncompressed = FileUtils.readFileToByteArray(fileToPack);
    if (DEBUG) {
        System.out.println("read " + uncompressed.length + " bytes from file.");
    }
    final ByteArrayOutputStream compressedStream = new ByteArrayOutputStream();
    //        final GZIPOutputStream gzipStream = new GZIPOutputStream(compressedStream);
    //        gzipStream.write(uncompressed);
    //        gzipStream.flush();
    final CRC32 checksum = new CRC32();
    checksum.update(uncompressed);
    final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    deflater.setInput(uncompressed);
    deflater.finish();
    final byte[] buf = new byte[Short.MAX_VALUE];
    while (!deflater.needsInput()) {
        final int bytesRead = deflater.deflate(buf);
        compressedStream.write(buf, 0, bytesRead);
    }

    final byte[] compressed = compressedStream.toByteArray();
    if (DEBUG) {
        System.out.println("compressed to " + compressed.length + " bytes.");
    }

    //        final Inflater decompresser = new Inflater();
    //        decompresser.setInput(compressed);
    //        byte[] result = new byte[5000];
    //        try {
    //            int resultLength = decompresser.inflate(result);
    //            final String str = new String(result, 0, resultLength);
    //            int i = 42;
    //        } catch (DataFormatException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //        decompresser.end();

    compressedFilesBaos.write(compressed);
    writeIntLE(fileEntriesBaos, compressed.length);

    writeIntLE(fileEntriesBaos, checksum.getValue());

    // bits: 0x00001000, gzip
    fileEntriesBaos.write(0);
    fileEntriesBaos.write(0x10);
    fileEntriesBaos.write(0);
    fileEntriesBaos.write(0);

    // 0 bytes manifest
    writeIntLE(fileEntriesBaos, 0);
}

From source file:org.roda_project.commons_ip.utils.ZIPUtils.java

public static void zip(Map<String, ZipEntryInfo> files, OutputStream out, SIP sip, boolean createSipIdFolder,
        boolean isCompressed) throws IOException, InterruptedException, IPException {
    ZipOutputStream zos = new ZipOutputStream(out);
    if (isCompressed) {
        zos.setLevel(Deflater.DEFAULT_COMPRESSION);
    } else {//w  w w  . j  a  va 2 s  .  c  om
        zos.setLevel(Deflater.NO_COMPRESSION);
    }

    Set<String> nonMetsChecksumAlgorithms = new TreeSet<>();
    nonMetsChecksumAlgorithms.add(IPConstants.CHECKSUM_ALGORITHM);
    Set<String> metsChecksumAlgorithms = new TreeSet<>();
    metsChecksumAlgorithms.addAll(nonMetsChecksumAlgorithms);
    metsChecksumAlgorithms.addAll(sip.getExtraChecksumAlgorithms());

    int i = 0;
    for (ZipEntryInfo file : files.values()) {
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }

        file.prepareEntryforZipping();

        LOGGER.debug("Zipping file {}", file.getFilePath());
        ZipEntry entry;
        if (createSipIdFolder) {
            entry = new ZipEntry(sip.getId() + "/" + file.getName());
        } else {
            entry = new ZipEntry(file.getName());
        }

        zos.putNextEntry(entry);
        InputStream inputStream = Files.newInputStream(file.getFilePath());

        try {
            Map<String, String> checksums;
            if (file instanceof METSZipEntryInfo) {
                checksums = calculateChecksums(Optional.of(zos), inputStream, metsChecksumAlgorithms);
                METSZipEntryInfo metsEntry = (METSZipEntryInfo) file;
                metsEntry.setChecksums(checksums);
                metsEntry.setSize(metsEntry.getFilePath().toFile().length());
            } else {
                checksums = calculateChecksums(Optional.of(zos), inputStream, nonMetsChecksumAlgorithms);
            }

            LOGGER.debug("Done zipping file");
            String checksum = checksums.get(IPConstants.CHECKSUM_ALGORITHM);
            String checksumType = IPConstants.CHECKSUM_ALGORITHM;
            file.setChecksum(checksum);
            file.setChecksumAlgorithm(checksumType);
            if (file instanceof METSFileTypeZipEntryInfo) {
                METSFileTypeZipEntryInfo f = (METSFileTypeZipEntryInfo) file;
                f.getMetsFileType().setCHECKSUM(checksum);
                f.getMetsFileType().setCHECKSUMTYPE(checksumType);
            } else if (file instanceof METSMdRefZipEntryInfo) {
                METSMdRefZipEntryInfo f = (METSMdRefZipEntryInfo) file;
                f.getMetsMdRef().setCHECKSUM(checksum);
                f.getMetsMdRef().setCHECKSUMTYPE(checksumType);
            }
        } catch (NoSuchAlgorithmException e) {
            LOGGER.error("Error while zipping files", e);
        }
        zos.closeEntry();
        inputStream.close();
        i++;

        sip.notifySipBuildPackagingCurrentStatus(i);
    }

    zos.close();
    out.close();
}