Using Java's own class java.util.zip for file/directory compression, there is a lack of support for Chinese name/directory naming, if there is a Chinese name, then packaging will fail. I after a period of exploration and practice, found in the General Ant.jar Class Library also has a zip class, import org.apache.tools.zip.*, can solve the problem that does not support the Chinese file directory, at the same time, ant access to more ways, The general application server has this package, it is really not to download a tomcat5. X, there are also Ant.jar, I have been tested, you can use.
Examples are as follows:
import java.io.File;
import org.apache.tools.zip.ZipOutputStream;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import dianda.cwmanage.*;
Import Dianda.com.util.Format;
public class Compressbook {
Public compressbook () {
}
public void Zip (String inputfilename) throws Excep tion {
String zipfilename= "c:\\test.zip";//packaged file name
System.out.println (zipfilename);
Zip (zipfilename, new File (InputFileName));
}
private void Zip (String zipfilename, File inputfile) throws Exception {
Zipoutputstream out = new ZIPOUTPU Tstream (New FileOutputStream (Zipfilename));
Zip (out, Inputfile, "");
System.out.println ("Zip done");
Out.close ();
}
private void Zip (Zipoutputstream out, File F, String Base) throws Exception {
if (f.isdirectory ()) {
file[] fl = F.listfiles ();
Out.putnextentry (new Org.apache.tools.zip.ZipEntry (base + "/"));
Base = base.length () = 0? "": Base + "/";
for (int i = 0; i < fl.length i++) {
Zip (out, fl[i], base + fl[i].getname ());
}
Else {
Out.putnextentry (new Org.apache.tools.zip.ZipEntry (base));
FileInputStream in = new FileInputStream (f);
Int b;
System.out.println (base);
while ((b = in.read ())!=-1) {
Out.write (b);
}
In.close ();
}
}
}
The only regret, in the ointment, is that, whatever, Java.util.zip or org.apache.tools.zip can not extract RAR tools into the RAR bag, search for a half-day also can not find the use of Java Decompression RAR tool into the bag, hoping which person can provide decompression RAR tool into the package, and you share!