Zipping a directory

K

Kasper K

Hi,

Simple question. I want to zip a directory using
java.util.zip.ZipOutputStream. I use the code below. My problem is
that ZipEntry dosnt seems to like the danish chars "æ ø å". So when I
unzip my compressed directory the files containing those chars has
substituded them with something like "-(_/&%".

private void zipDir(File dir, ZipOutputStream out) throws Exception
{
int BUFFER = 2048;
byte data[] = new byte[BUFFER];
File files[] = dir.listFiles();

for (int i=0; i<files.length; i++)
{
if(files.isDirectory())
{
zipDir(files, out);
} else
{
FileInputStream fi = new FileInputStream(files);
BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);

String name = files.getPath();
name = name.substring(3);
//System.out.println(name);

ZipEntry entry = new ZipEntry(name);
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0, BUFFER)) != -1)
{
out.write(data, 0, count);
}
origin.close();
out.closeEntry();
}
}
}

Kasper, Denmark
 
H

Harald Hein

Kasper K said:
Simple question. I want to zip a directory using
java.util.zip.ZipOutputStream. I use the code below. My problem is
that ZipEntry dosnt seems to like the danish chars "æ ø å". So
when I unzip my compressed directory the files containing those
chars has substituded them with something like "-(_/&%".

Your description is unclear (is the file content or the file name
corrupted). So just some hints:

Get a debugger and figure out if the data gets messed up during
compression or uncompression.

Check the zip standard. You will find that only 8 bit chars are
officially supported. Sun does a UTF-8 encoding for other chars. If you
use an unzipper which isn't aware of this, you get strange characters.

Check Sun's bugparade. There was an issue with incorrect UTF-8 encoding
/decoding of file names in the zip package.

String name = files.getPath();
name = name.substring(3);


? Workaround for Windows? if yes, check the File class for better
methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top