Reputation: 215
I'm using a Java mechanism to extract zip files. The mechanism works fine if there is no files in it with accents on title. Since I'm from portugal, chars like ã, ç, õ, é, etc. are usually used in my language. If any of this chars are in the filename, an IO exception occurs.
while (zipFileEntries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
File destFile = new File(unzipDestinationDirectory, currentEntry);
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
// extract file if not a directory
if (!entry.isDirectory()) {
BufferedInputStream is =
new BufferedInputStream(zip_file.getInputStream(entry));
int currentByte;
byte data[] = new byte[BUFFER];
// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
It crashes on while((currentByte = is.read(data, 0, BUFFER)) != -1)
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:134)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at parsers.ZipParser.decompressZipFile(ZipParser.java:83)
at poc.MainPOC.main(MainPOC.java:61)
Are you aware of any workaround to deal with this problem? Can I change the filename of a file inside a zip without decompressing it?
Upvotes: 1
Views: 2988
Reputation: 912
Since Java 7, there is a way new construcor to ZipInputStream
to specify a charset to use for the filename. See the documentation here.
So you would create your ZipInputStream
with something like :
ZipInputStream zis = new ZipInputStream(new FileInputStream("your zip file"), Charset.forName("Encoding here"));
See Charset to have a bit more informations about how to use it.
It will not change the way you are reading the file, so you will need another workaround to read the content. But for more information, see this answer Java zip character encoding, you can probably re-use some of the code.
Upvotes: 3
Reputation: 309018
I think you must set the encoding properly when you compress and decompress. Did you make it UTF-8 when you created the ZIP file? If not, I'd recommend trying it.
Upvotes: 0