Zipping files that contain special characters

From UbikWiki

(Difference between revisions)
(Dependency)
(Usage)

Line 1:

Line 1:

== Usage ==
== Usage ==
-
You want to zip files that contain international characters (é,ç,è, chinese characters)
+
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).
Default Java ZipFileOutputStream will not allow you to do it (unless until JDK 5).
 +
== Dependency ==
== Dependency ==
Use [[https://truezip.dev.java.net/ TrueZip]]
Use [[https://truezip.dev.java.net/ TrueZip]]

Revision as of 22:33, 19 November 2007

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)
                {
                    ;
                }
            }
        }
    }
Personal tools