Example usage for java.util.zip Deflater NO_COMPRESSION

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

Introduction

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

Prototype

int NO_COMPRESSION

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

Click Source Link

Document

Compression level for no compression.

Usage

From source file:be.ibridge.kettle.job.entry.zipfile.JobEntryZipFile.java

public Result execute(Result prev_result, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = new Result(nr);
    result.setResult(false);/*from w  ww.ja  v  a  2  s .  c  o  m*/
    boolean Fileexists = false;

    String realZipfilename = StringUtil.environmentSubstitute(zipFilename);
    String realWildcard = StringUtil.environmentSubstitute(wildcard);
    String realWildcardExclude = StringUtil.environmentSubstitute(wildcardexclude);
    String realTargetdirectory = StringUtil.environmentSubstitute(sourcedirectory);
    String realMovetodirectory = StringUtil.environmentSubstitute(movetodirectory);

    if (realZipfilename != null) {
        FileObject fileObject = null;
        try {
            fileObject = KettleVFS.getFileObject(realZipfilename);
            // Check if Zip File exists
            if (fileObject.exists()) {
                Fileexists = true;
                log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileExists1.Label")
                        + realZipfilename + Messages.getString("JobZipFiles.Zip_FileExists2.Label"));
            } else {
                Fileexists = false;
            }

            // Let's start the process now
            if (ifzipfileexists == 3 && Fileexists) {
                // the zip file exists and user want to Fail
                result.setResult(false);
                result.setNrErrors(1);

            } else if (ifzipfileexists == 2 && Fileexists) {
                // the zip file exists and user want to do nothing
                result.setResult(true);

            } else if (afterzip == 2 && realMovetodirectory == null) {
                // After Zip, Move files..User must give a destination Folder
                result.setResult(false);
                result.setNrErrors(1);
                log.logError(toString(),
                        Messages.getString("JobZipFiles.AfterZip_No_DestinationFolder_Defined.Label"));

            } else
            // After Zip, Move files..User must give a destination Folder
            {

                if (ifzipfileexists == 0 && Fileexists) {

                    // the zip file exists and user want to create new one with unique name
                    //Format Date

                    DateFormat dateFormat = new SimpleDateFormat("hhmmss_mmddyyyy");
                    realZipfilename = realZipfilename + "_" + dateFormat.format(new Date()) + ".zip";
                    log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileNameChange1.Label")
                            + realZipfilename + Messages.getString("JobZipFiles.Zip_FileNameChange1.Label"));

                } else if (ifzipfileexists == 1 && Fileexists) {
                    log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileAppend1.Label")
                            + realZipfilename + Messages.getString("JobZipFiles.Zip_FileAppend2.Label"));
                }

                // Get all the files in the directory...

                File f = new File(realTargetdirectory);

                String[] filelist = f.list();

                log.logDetailed(toString(),
                        Messages.getString("JobZipFiles.Files_Found1.Label") + filelist.length
                                + Messages.getString("JobZipFiles.Files_Found2.Label") + realTargetdirectory
                                + Messages.getString("JobZipFiles.Files_Found3.Label"));

                Pattern pattern = null;
                if (!Const.isEmpty(realWildcard)) {
                    pattern = Pattern.compile(realWildcard);

                }
                Pattern patternexclude = null;
                if (!Const.isEmpty(realWildcardExclude)) {
                    patternexclude = Pattern.compile(realWildcardExclude);

                }

                // Prepare Zip File
                byte[] buffer = new byte[18024];

                FileOutputStream dest = new FileOutputStream(realZipfilename);
                BufferedOutputStream buff = new BufferedOutputStream(dest);
                ZipOutputStream out = new ZipOutputStream(buff);

                // 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...)
                String[] ZippedFiles = new String[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!
                    if (pattern != null) {
                        Matcher matcher = pattern.matcher(filelist[i]);
                        getIt = matcher.matches();
                    }

                    if (patternexclude != null) {
                        Matcher matcherexclude = patternexclude.matcher(filelist[i]);
                        getItexclude = matcherexclude.matches();
                    }

                    // Get processing File
                    String targetFilename = realTargetdirectory + Const.FILE_SEPARATOR + filelist[i];
                    File file = new File(targetFilename);

                    if (getIt && !getItexclude && !file.isDirectory()) {

                        // We can add the file to the Zip Archive

                        log.logDebug(toString(),
                                Messages.getString("JobZipFiles.Add_FilesToZip1.Label") + filelist[i]
                                        + Messages.getString("JobZipFiles.Add_FilesToZip2.Label")
                                        + realTargetdirectory
                                        + Messages.getString("JobZipFiles.Add_FilesToZip3.Label"));

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

                        // Add ZIP entry to output stream.
                        out.putNextEntry(new ZipEntry(filelist[i]));

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

                        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();

                //-----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 File
                            FileObject fileObjectd = KettleVFS
                                    .getFileObject(realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i]);

                            // Here we can move, delete files
                            if (afterzip == 1) {
                                // Delete File
                                boolean deleted = fileObjectd.delete();
                                if (!deleted) {
                                    result.setResult(false);
                                    result.setNrErrors(1);
                                    log.logError(toString(),
                                            Messages.getString("JobZipFiles.Cant_Delete_File1.Label")
                                                    + realTargetdirectory + Const.FILE_SEPARATOR
                                                    + ZippedFiles[i] + Messages
                                                            .getString("JobZipFiles.Cant_Delete_File2.Label"));

                                }
                                // File deleted
                                log.logDebug(toString(),
                                        Messages.getString("JobZipFiles.File_Deleted1.Label")
                                                + realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i]
                                                + Messages.getString("JobZipFiles.File_Deleted2.Label"));
                            } else if (afterzip == 2) {
                                // Move File   
                                try {
                                    FileObject fileObjectm = KettleVFS.getFileObject(
                                            realMovetodirectory + Const.FILE_SEPARATOR + ZippedFiles[i]);
                                    fileObjectd.moveTo(fileObjectm);
                                } catch (IOException e) {
                                    log.logError(toString(),
                                            Messages.getString("JobZipFiles.Cant_Move_File1.Label")
                                                    + ZippedFiles[i]
                                                    + Messages.getString("JobZipFiles.Cant_Move_File2.Label")
                                                    + e.getMessage());
                                    result.setResult(false);
                                    result.setNrErrors(1);
                                }
                                // File moved
                                log.logDebug(toString(), Messages.getString("JobZipFiles.File_Moved1.Label")
                                        + ZippedFiles[i] + Messages.getString("JobZipFiles.File_Moved2.Label"));
                            }
                        }
                    }
                }
                result.setResult(true);
            }
        } catch (IOException e) {
            log.logError(toString(),
                    Messages.getString("JobZipFiles.Cant_CreateZipFile1.Label") + realZipfilename
                            + Messages.getString("JobZipFiles.Cant_CreateZipFile2.Label") + e.getMessage());
            result.setResult(false);
            result.setNrErrors(1);
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                } catch (IOException ex) {
                }
                ;
            }
        }
    } else {
        result.setResult(false);
        result.setNrErrors(1);
        log.logError(toString(), Messages.getString("JobZipFiles.No_ZipFile_Defined.Label"));
    }

    return result;
}

From source file:com.threerings.media.tile.bundle.tools.TileSetBundler.java

/**
 * Create a tileset bundle jar file.//from   w ww.ja va2 s. c  o  m
 *
 * @param target the tileset bundle file that will be created.
 * @param bundle contains the tilesets we'd like to save out to the bundle.
 * @param improv the image provider.
 * @param imageBase the base directory for getting images for non-ObjectTileSet tilesets.
 * @param keepOriginalPngs bundle up the original PNGs as PNGs instead of converting to the
 * FastImageIO raw format
 */
public static boolean createBundleJar(File target, TileSetBundle bundle, ImageProvider improv, String imageBase,
        boolean keepOriginalPngs, boolean uncompressed) throws IOException {
    // now we have to create the actual bundle file
    FileOutputStream fout = new FileOutputStream(target);
    Manifest manifest = new Manifest();
    JarOutputStream jar = new JarOutputStream(fout, manifest);
    jar.setLevel(uncompressed ? Deflater.NO_COMPRESSION : Deflater.BEST_COMPRESSION);

    try {
        // write all of the image files to the bundle, converting the
        // tilesets to trimmed tilesets in the process
        Iterator<Integer> iditer = bundle.enumerateTileSetIds();

        // Store off the updated TileSets in a separate Map so we can wait to change the
        // bundle till we're done iterating.
        HashIntMap<TileSet> toUpdate = new HashIntMap<TileSet>();
        while (iditer.hasNext()) {
            int tileSetId = iditer.next().intValue();
            TileSet set = bundle.getTileSet(tileSetId);
            String imagePath = set.getImagePath();

            // sanity checks
            if (imagePath == null) {
                log.warning("Tileset contains no image path " + "[set=" + set + "]. It ain't gonna work.");
                continue;
            }

            // if this is an object tileset, trim it
            if (!keepOriginalPngs && (set instanceof ObjectTileSet)) {
                // set the tileset up with an image provider; we
                // need to do this so that we can trim it!
                set.setImageProvider(improv);

                // we're going to trim it, so adjust the path
                imagePath = adjustImagePath(imagePath);
                jar.putNextEntry(new JarEntry(imagePath));

                try {
                    // create a trimmed object tileset, which will
                    // write the trimmed tileset image to the jar
                    // output stream
                    TrimmedObjectTileSet tset = TrimmedObjectTileSet.trimObjectTileSet((ObjectTileSet) set,
                            jar);
                    tset.setImagePath(imagePath);
                    // replace the original set with the trimmed
                    // tileset in the tileset bundle
                    toUpdate.put(tileSetId, tset);

                } catch (Exception e) {
                    e.printStackTrace(System.err);

                    String msg = "Error adding tileset to bundle " + imagePath + ", " + set.getName() + ": "
                            + e;
                    throw (IOException) new IOException(msg).initCause(e);
                }

            } else {
                // read the image file and convert it to our custom
                // format in the bundle
                File ifile = new File(imageBase, imagePath);
                try {
                    BufferedImage image = ImageIO.read(ifile);
                    if (!keepOriginalPngs && FastImageIO.canWrite(image)) {
                        imagePath = adjustImagePath(imagePath);
                        jar.putNextEntry(new JarEntry(imagePath));
                        set.setImagePath(imagePath);
                        FastImageIO.write(image, jar);
                    } else {
                        jar.putNextEntry(new JarEntry(imagePath));
                        FileInputStream imgin = new FileInputStream(ifile);
                        StreamUtil.copy(imgin, jar);
                    }
                } catch (Exception e) {
                    String msg = "Failure bundling image " + ifile + ": " + e;
                    throw (IOException) new IOException(msg).initCause(e);
                }
            }
        }
        bundle.putAll(toUpdate);

        // now write a serialized representation of the tileset bundle
        // object to the bundle jar file
        JarEntry entry = new JarEntry(BundleUtil.METADATA_PATH);
        jar.putNextEntry(entry);
        ObjectOutputStream oout = new ObjectOutputStream(jar);
        oout.writeObject(bundle);
        oout.flush();

        // finally close up the jar file and call ourself done
        jar.close();

        return true;

    } catch (Exception e) {
        // remove the incomplete jar file and rethrow the exception
        jar.close();
        if (!target.delete()) {
            log.warning("Failed to close botched bundle '" + target + "'.");
        }
        String errmsg = "Failed to create bundle " + target + ": " + e;
        throw (IOException) new IOException(errmsg).initCause(e);
    }
}

From source file:com.panet.imeta.job.entries.zipfile.JobEntryZipFile.java

public boolean processRowFile(Job parentJob, Result result, String realZipfilename, String realWildcard,
        String realWildcardExclude, String realTargetdirectory, String realMovetodirectory,
        boolean createparentfolder) {
    LogWriter log = LogWriter.getInstance();
    boolean Fileexists = false;
    File tempFile = null;/* w  w  w.j av  a 2 s  .  c o 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;
    FileOutputStream dest = null;
    BufferedOutputStream buff = null;
    org.apache.tools.zip.ZipOutputStream out = null;
    org.apache.tools.zip.ZipEntry entry = null;

    try {
        OriginFile = KettleVFS.getFileObject(realTargetdirectory);
        orginexist = OriginFile.exists();
    } catch (Exception e) {
    } finally {
        if (OriginFile != null) {
            try {
                OriginFile.close();
            } catch (IOException ex) {
            }
            ;
        }
    }

    if (realZipfilename != null && orginexist) {

        FileObject fileObject = null;
        try {
            fileObject = KettleVFS.getFileObject(realZipfilename);

            // Check if Zip File exists
            if (fileObject.exists()) {
                Fileexists = true;
                if (log.isDebug())
                    log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileExists1.Label")
                            + realZipfilename + Messages.getString("JobZipFiles.Zip_FileExists2.Label"));
            }
            // Let's see if we need to create parent folder of destination
            // zip filename
            if (createparentfolder) {
                createParentFolder(realZipfilename);
            }

            // 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,
                            KettleVFS.getFileObject(realZipfilename), 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;
                log.logError(toString(),
                        Messages.getString("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
                String[] filelist = null;

                File f = new File(realTargetdirectory);
                if (f.isDirectory()) {
                    // Target is a directory
                    // Get all the files in the directory...
                    filelist = f.list();
                } else {
                    // Target is a file
                    filelist = new String[1];
                    filelist[0] = f.getName();
                }
                if (filelist.length == 0) {
                    resultat = false;
                    log.logError(toString(),
                            Messages.getString("JobZipFiles.Log.FolderIsEmpty", realTargetdirectory));
                } else if (!checkContainsFile(realTargetdirectory, filelist)) {
                    resultat = false;
                    log.logError(toString(),
                            Messages.getString("JobZipFiles.Log.NoFilesInFolder", realTargetdirectory));
                } 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 (realZipfilename.toLowerCase().endsWith(".zip")) {
                            // strip this off
                            realZipfilename = realZipfilename.substring(0, realZipfilename.length() - 4);
                        }

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

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

                        renameOk = fileZip.renameTo(tempFile);

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

                    if (log.isDetailed())
                        log.logDetailed(toString(), Messages.getString("JobZipFiles.Files_Found1.Label")
                                + filelist.length + Messages.getString("JobZipFiles.Files_Found2.Label")
                                + realTargetdirectory + Messages.getString("JobZipFiles.Files_Found3.Label"));

                    Pattern pattern = null;
                    Pattern patternexclude = null;
                    // Let's prepare pattern..only if target is a folder !
                    if (f.isDirectory()) {
                        if (!Const.isEmpty(realWildcard)) {
                            pattern = Pattern.compile(realWildcard);
                        }

                        if (!Const.isEmpty(realWildcardExclude)) {
                            patternexclude = Pattern.compile(realWildcardExclude);
                        }
                    }

                    // Prepare Zip File
                    buffer = new byte[18024];
                    dest = new FileOutputStream(realZipfilename);
                    buff = new BufferedOutputStream(dest);
                    out = new org.apache.tools.zip.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 = (ZipEntry) 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 = (ZipEntry) zin.getNextEntry();
                        }
                        // Close the streams
                        zin.close();
                    }

                    // Set the method
                    out.setMethod(org.apache.tools.zip.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...)
                    String[] ZippedFiles = new String[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 (f.isDirectory()) {
                            if (pattern != null) {
                                Matcher matcher = pattern.matcher(filelist[i]);
                                getIt = matcher.matches();
                            }

                            if (patternexclude != null) {
                                Matcher matcherexclude = patternexclude.matcher(filelist[i]);
                                getItexclude = matcherexclude.matches();
                            }
                        }
                        // Get processing File
                        String targetFilename = realTargetdirectory + Const.FILE_SEPARATOR + filelist[i];
                        if (f.isFile())
                            targetFilename = realTargetdirectory;

                        File file = new File(targetFilename);

                        if (getIt && !getItexclude && !file.isDirectory() && !fileSet.contains(filelist[i])) {

                            // We can add the file to the Zip Archive
                            if (log.isDebug())
                                log.logDebug(toString(),
                                        Messages.getString("JobZipFiles.Add_FilesToZip1.Label") + filelist[i]
                                                + Messages.getString("JobZipFiles.Add_FilesToZip2.Label")
                                                + realTargetdirectory
                                                + Messages.getString("JobZipFiles.Add_FilesToZip3.Label"));

                            // Associate a file input stream for the current
                            // file
                            FileInputStream in = new FileInputStream(targetFilename);
                            // Add ZIP entry to output stream.
                            out.putNextEntry(new ZipEntry(filelist[i]));

                            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())
                        log.logBasic(toString(), Messages.getString("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 File
                                FileObject fileObjectd = KettleVFS.getFileObject(
                                        realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i]);
                                if (f.isFile())
                                    fileObjectd = KettleVFS.getFileObject(realTargetdirectory);

                                // Here gc() is explicitly called if e.g.
                                // createfile is used in the same
                                // job for the same file. The problem is
                                // that after creating the file the
                                // file object is not properly garbaged
                                // collected and thus the file cannot
                                // be deleted anymore. This is a known
                                // problem in the JVM.

                                System.gc();

                                // Here we can move, delete files
                                if (afterzip == 1) {

                                    // Delete File
                                    boolean deleted = fileObjectd.delete();
                                    if (!deleted) {
                                        resultat = false;
                                        log.logError(toString(), Messages
                                                .getString("JobZipFiles.Cant_Delete_File1.Label")
                                                + realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i]
                                                + Messages.getString("JobZipFiles.Cant_Delete_File2.Label"));

                                    }
                                    // File deleted
                                    if (log.isDebug())
                                        log.logDebug(toString(),
                                                Messages.getString("JobZipFiles.File_Deleted1.Label")
                                                        + realTargetdirectory + Const.FILE_SEPARATOR
                                                        + ZippedFiles[i] + Messages
                                                                .getString("JobZipFiles.File_Deleted2.Label"));
                                } else if (afterzip == 2) {
                                    // Move File
                                    try {
                                        FileObject fileObjectm = KettleVFS.getFileObject(
                                                realMovetodirectory + Const.FILE_SEPARATOR + ZippedFiles[i]);
                                        fileObjectd.moveTo(fileObjectm);
                                    } catch (IOException e) {
                                        log.logError(toString(),
                                                Messages.getString("JobZipFiles.Cant_Move_File1.Label")
                                                        + ZippedFiles[i]
                                                        + Messages
                                                                .getString("JobZipFiles.Cant_Move_File2.Label")
                                                        + e.getMessage());
                                        resultat = false;
                                    }
                                    // File moved
                                    if (log.isDebug())
                                        log.logDebug(toString(),
                                                Messages.getString("JobZipFiles.File_Moved1.Label")
                                                        + ZippedFiles[i]
                                                        + Messages.getString("JobZipFiles.File_Moved2.Label"));
                                }
                            }
                        }
                    }

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

                    resultat = true;
                }
            }
        } catch (Exception e) {
            log.logError(toString(),
                    Messages.getString("JobZipFiles.Cant_CreateZipFile1.Label") + realZipfilename
                            + Messages.getString("JobZipFiles.Cant_CreateZipFile2.Label") + e.getMessage());
            // result.setResult( false );
            // result.setNrErrors(1);
            resultat = false;
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                } catch (IOException ex) {
                }
                ;
            }
            // Close the ZipOutPutStream
            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) {
            }
            ;
        }
    } else {
        resultat = true;
        if (realZipfilename == null)
            log.logError(toString(), Messages.getString("JobZipFiles.No_ZipFile_Defined.Label"));
        if (!orginexist)
            log.logError(toString(),
                    Messages.getString("JobZipFiles.No_FolderCible_Defined.Label", realTargetdirectory));
    }
    // return a verifier
    return resultat;
}

From source file:com.cellbots.logger.LoggerActivity.java

@Override
public void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
    super.onPrepareDialog(id, dialog, bundle);

    if (id != PROGRESS_ID) {
        return;/* w  w w  . ja  v a2 s. co  m*/
    }

    final ProgressDialog progressDialog = (ProgressDialog) dialog;
    progressDialog.setMessage("Processing...");
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            int done = msg.getData().getInt("percentageDone");
            String status = msg.getData().getString("status");
            progressDialog.setProgress(done);
            progressDialog.setMessage(status);

            if (mRemoteControl != null) {
                mRemoteControl.broadcastMessage("Zipping Progress: " + done + "%\n");
            }
        }
    };

    zipperThread = new Thread() {
        @Override
        public void run() {
            if (useZip) {
                ZipItUpRequest request = new ZipItUpRequest();
                String directoryName = application.getLoggerPathPrefix();
                request.setInputFiles(new FileListFetcher().getFilesAndDirectoriesInDir(directoryName));
                request.setOutputFile(directoryName + "/logged-data.zip");
                request.setMaxOutputFileSize(MAX_OUTPUT_ZIP_CHUNK_SIZE);
                request.setDeleteInputfiles(true);
                request.setCompressionLevel(Deflater.NO_COMPRESSION);

                try {
                    new ZipItUpProcessor(request).chunkIt(handler);
                } catch (IOException e) {
                    Log.e("Oh Crap!", "IoEx", e);
                }
            }
            // closing dialog
            progressDialog.dismiss();
            application.generateNewFilePathUniqueIdentifier();

            // TODO: Need to deal with empty directories that are created if
            // another recording
            // session is never started.
            initSensorLogFiles();

            if (mCamcorderView != null) {
                try {
                    mCamcorderView.startPreview();
                    if (mRemoteControl != null)
                        mRemoteControl.broadcastMessage("*** Packaging Finished: OK to start ***\n");
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        // @Override
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Camera hardware error. Please restart the application.", Toast.LENGTH_LONG)
                                    .show();
                        }
                    });
                    finish();
                    return;
                }
            }
        }
    };
    zipperThread.start();
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void jarThatUsesCustomCompressionConfigurationCanBeRepackaged() throws IOException {
    File source = this.temporaryFolder.newFile("source.jar");
    ZipOutputStream output = new ZipOutputStream(new FileOutputStream(source)) {
        {//from   www  .  j  a  v a2 s  .co  m
            this.def = new Deflater(Deflater.NO_COMPRESSION, true);
        }
    };
    byte[] data = new byte[1024 * 1024];
    new Random().nextBytes(data);
    ZipEntry entry = new ZipEntry("entry.dat");
    output.putNextEntry(entry);
    output.write(data);
    output.closeEntry();
    output.close();
    File dest = this.temporaryFolder.newFile("dest.jar");
    Repackager repackager = new Repackager(source);
    repackager.setMainClass("com.example.Main");
    repackager.repackage(dest, NO_LIBRARIES);
}

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  ww . j  ava  2  s  .  com*/
    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:com.thoughtworks.go.server.controller.ArtifactsControllerIntegrationTest.java

private ModelAndView postZipFolderFromTmp(File root, String folder) throws Exception {
    File source = file(root, "/tmp" + folder);
    File zippedFile = zipUtil.zip(source, TestFileUtil.createUniqueTempFile(source.getName()),
            Deflater.NO_COMPRESSION);
    zippedFile.deleteOnExit();//from   w  ww. j a v  a 2 s . co  m
    return postFile("", "zipfile", new FileInputStream(zippedFile), response);
}