The Zip method zippath the parameter to save the zip file path Srcpath parameter to the directory that needs to be compressed test no problem on Linux Windows! The main coding problem is more trouble ~ if there are other anomalies please leave a message or have a better way to welcome more comments
Zip Zhe folder
void Zip (String zippath, String Srcpath,javax.servlet.jsp.jspwriter out) throws Exception {
FileOutputStream output = null;
Zipoutputstream zipoutput = null;
try{
Output = new FileOutputStream (Zippath);
Zipoutput = new Zipoutputstream (output);
ZipEntry (Zipoutput,srcpath,srcpath,zippath);
}catch (Exception e) {
Out.print ("File zip Error");
}finally{
if (zipoutput!=null) zipoutput.close ();
}
Out.print ("Zip OK" +zippath);
}
Add the Zip entry
void ZipEntry (Zipoutputstream zipos, String initpath,string filepath,string zippath) throws Exception {
String entryname = FilePath;
File F = new file (FilePath);
if (F.isdirectory ()) {//??
string[] files = f.list ();
for (int i = 0; i < files.length; i++)
ZipEntry (Zipos, Initpath, FilePath + file.separator + files[i],zippath);
Return
}
String chph = initpath.substring (Initpath.lastindexof ("/") + 1);/?????
int Idx=initpath.lastindexof (CHPH);
if (idx!=-1) {
EntryName = filepath.substring (idx);
}
ZipEntry entry;
Entry = new ZipEntry (entryname);
File FF = new file (FilePath);
if (Ff.getabsolutepath (). Equals (Zippath)) return;
Entry.setsize (Ff.length ());
Entry.settime (Ff.lastmodified ());
The CRC efficacy
ENTRY.SETCRC (0);
CRC32 CRC = New CRC32 ();
Crc.reset ();
Zipos.putnextentry (entry);
int len = 0;
byte[] buffer = new byte[2048];
int bufferlen = 2048;
FileInputStream input =null;
try{
input = new FileInputStream (FilePath);
while (len = input.read (buffer, 0, Bufferlen))!=-1) {
Zipos.write (buffer, 0, Len);
Crc.update (buffer, 0, Len);
}
}catch (Exception e) {
}finally{
if (input!=null) input.close ();
}
ENTRY.SETCRC (Crc.getvalue ());
}