Zipping files that contain special characters
From UbikWiki
(Difference between revisions)
(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...) |
m (add category) |
||
(3 intermediate revisions not shown.) | |||
Line 1: |
Line 1: | ||
+ | |||
+ | == 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 == | == Dependency == | ||
Use [[https://truezip.dev.java.net/ TrueZip]] | Use [[https://truezip.dev.java.net/ TrueZip]] | ||
Line 51: |
Line 57: | ||
} | } | ||
}</source> | }</source> | ||
+ | |||
+ | == Authors == | ||
+ | * Philippe Mouawad | ||
+ | |||
+ | == History == | ||
+ | <pageby/> | ||
+ | [[Category:Tips and Tricks]] | ||
+ | [[Category:I/O developpement]] |
Current revision
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