JSP above the implementation of directory compression

Source: Internet
Author: User
Tags crc32 zip

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 ());

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.