Zipping files that contain special characters

From UbikWiki

Contents

Usage

You want to zip files that contain international characters (é,ç,è, chinese characters).

Default Java ZipFileOutputStream will not allow you to do it (unless until JDK 5).

Dependency

Use [TrueZip]

Code

Java code to zip an array of files:

/**
     * Zips a set of files or folders using the base folder 
     * @param outFile Target zip
     * @param inputFiles Files or folders to be included in the zip
     * @return File generated file
     * @throws TechniqueException
     */
    public static final File zipFiles(File outFile, File[] inputFiles) throws TechniqueException
    {
    	ZipOutputStream out = null;
 
        try
        {
            File base = computeBaseFolder(inputFiles);
            if (base != null)
            {
                base = base.isDirectory() ? base : base.getParentFile();
            }
            // Create the ZIP file
            out = new ZipOutputStream(new FileOutputStream(outFile
                    .getAbsolutePath()), "IBM437");
            zipFilesIntoStream(base, out, inputFiles);
 
            return outFile;
        }
        catch(IOException e)
        {
            throw new TechniqueException(
                    "Error creating zip file " + outFile.getAbsolutePath() + " including files:" + 
                    Arrays.asList(inputFiles),e);
        }
        finally
        {
            if (out != null)
            {
                try
                {
                    out.close();
                }
                catch (IOException e1)
                {
                    ;
                }
            }
        }
    }

Authors

  • Philippe Mouawad

History

  • Page created by Pmouawad, 19 November 2007: New page: == Dependency == Use [[https://truezip.dev.java.net/ TrueZip]] == Code == Java code to zip an array of files: <source lang="java" > /** * Zips a set of files or folders using t...
  • Last modified by Abourre, 27 January 2009: add category
Personal tools