[Go] to implement file compression

Source: Internet
Author: User

/** Create a compressed file from the folder path, to the compressed file path for the Create good */public void Createzip (String from,string to) throws IOException {LIST&LT;FILE&G  T;list=getfiles (from);  Zipoutputstream out=new Zipoutputstream (New FileOutputStream (new File));  for (File f:list) {InputStream in=new fileinputstream (f);    String Name=getrelname (from,f);  ZipEntry en=new ZipEntry (New File (from) getName () + "/" +name);    En.setsize (F.length ());  Out.putnextentry (en);      Out.setcomment ("Chinese test");  int len=0;  Byte[]buffer=new byte[1024];  while ( -1!= (len=in.read (buffer))) {out.write (buffer, 0, Len);  } in.close ();  } out.close (); /** gets the relative path of the file */private string Getrelname (String From,file f) {//TODO auto-generated method stub String A=F.GETABSO  Lutepath (). replace (from+ "\ \", "");  A=a.replace ("\ \", "/");  System.out.println (from+ "---" +a);  return A; /** gets all the files under the path, including the */private list<file> getFiles (String sou) {list<file>list=new Arraylist<file&gt  ;();  File F=new file (SOU); File files[]=f.listFiles ();  for (file file:files) {if (File.isfile ()) {list.add (file);  } else {List.addall (GetFiles (File.getpath ()));  }} return list; }

##########################################

Transferred from: http://blog.csdn.net/liu149339750/article/details/7887701

##########################################

[Go] to implement file compression

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.