dcsimg
How can i zip a txt file including chinese using java.util.zip package?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ke_weili
Posted On:   Tuesday, November 19, 2002 05:40 PM

When i zip and unzip txt file without any other language other than english through java.util.zip,it works well. But if the txt file include other language(e.g. chinese), it will get wrong. How can i let it work well? Below is a brief code list. ZIP: FileOutputStream f=new FileOutputStream("e:/mytest.zip"); CheckedOutputStream csum=new CheckedOutputStream(f,new Adler32()); ZipOutputStream out=new ZipOutputStream( new BufferedOutputStream(csum)); out.setComment("A test of Java Zipping"); for(int i=0;i    More>>

When i zip and unzip txt file without any other language


other than english through java.util.zip,it works well.


But if the txt file include other language(e.g. chinese),


it will get wrong.


How can i let it work well?


Below is a brief code list.


ZIP:


FileOutputStream f=new FileOutputStream("e:/mytest.zip");


CheckedOutputStream csum=new CheckedOutputStream(f,new


Adler32());


ZipOutputStream out=new ZipOutputStream(


new BufferedOutputStream(csum));


out.setComment("A test of Java Zipping");


for(int i=0;i


if(loadFiles[i].isFile()){


temp+=1;


v.add(vectorInt++,loadFiles[i].getAbsoluteFile().t


oString());


}


else{


File[] loadFiles2=loadFiles[i].listFiles();


FileCopy(loadFiles2);


}


}


for(int k=0;k


System.out.println("Writing"+v.get(k).toString());


BufferedReader in=new BufferedReader(new FileReader


(v.get(k).toString()));


out.putNextEntry(new ZipEntry(v.get(k).toString


()));


int c;


while((c=in.read())!=-1)


out.write(c);


in.close();


}


out.close();


UNZIP:


FileInputStream fi=new FileInputStream


(saveFile.getAbsoluteFile().toString());


CheckedInputStream csumi=new CheckedInputStream(fi,new


Adler32());


ZipInputStream in2=new ZipInputStream(new


BufferedInputStream(csumi));


ZipEntry ze;


while((ze=in2.getNextEntry())!=null){


System.out.println("Reading file "+ze);


int x;


int index=0;


fileName=ze.getName();


index=fileName.lastIndexOf("\");


index=index+1;


fileName=fileName.substring(index);


System.out.println("real file Name = "+fileName);


fileName="d:\"+fileName;


BufferedWriter out2=new BufferedWriter(new FileWriter


(fileName));


while((x=in2.read())!=-1)


out2.write(x);


out2.close();


}


in2.close();

   <<Less

Re: How can i zip a txt file including chinese using java.util.zip package?

Posted By:   Stephen_Ostermiller  
Posted On:   Wednesday, November 20, 2002 03:45 AM

Your problem is with the line:
new FileReader (v.get(k).toString())


FileReader assumes that the file is in the character set of the platform you are using. This is probably not the case unless you are using a chinese computer.


You probably want something along the lines:
new InputStreamReader(new FileInputStream(v.get(k).toString()), "GB2312");


That is just a primer on how to read files into strings in an international fashion. It is not clear to me why you are using strings at all. Zip is meant to work on bytes. It seems to me that if you read in bytes and zipped bytes, you wouldn't have to worry about the contents of the file. The file would be zipped properly no matter what its character set is, or even if it is a binary file.

About | Sitemap | Contact