Java to zip file contains Chinese sub-file operations

Source: Internet
Author: User


Small fish to do file compression decompression, the beginning and no attention to detail, using the Java JDK Zip Operation Tool class

This causes the problem is the Chinese operation is garbled, or is broken.

Check the information to know this is 1.6 of the history of the problem

Fortunately, open source Apache provides a support package that we can use to get it over.

Here is the download link


Provide a jar package download link: http://download.csdn.net/detail/u010962482/8748091


Nonsense not much to say paste the decompression compression code.

/** * Unzip the file to the specified directory * @param zipfile * @param descdir * @author isea533 * @SuppressWarnings ("Rawtypes") public static void Unzipfiles (String zip_s_file,string descdir) {OutputStream     out= null;     InputStream in = null;             File zipfile= new file (Zip_s_file);         Determine if the decompression path exists and does not exist create file Pathfile = new file (Descdir);         if (!pathfile.exists ()) {pathfile.mkdirs ();           }//Create folder path to unzip ZipFile zip; try {
<span style= "White-space:pre" ></span>//note here the code according to its own situation to fill out the zip = new ZipFile (ZipFile, "GBK"); For (Enumeration entries = Zip.getentries (); entries.hasmoreelements ();) {ZipEntry entry = (Zi                  Pentry) entries.nextelement ();                  String zipentryname = Entry.getname ();                  in = Zip.getinputstream (entry);                  String Outpath = (descdir+zipentryname). ReplaceAll ("\\*", "/");; The output file path information, if the file is present, is overwritten if you do not want to overwrite the Boolean type System that modifies outputstream.                   Out.println (Outpath);                  out = new FileOutputStream (outpath,true);                  byte[] buf1 = new byte[1024];                  int Len;                  while ((Len=in.read (BUF1)) >0) {out.write (Buf1,0,len); }} System.                 Out.println ("****************** decompression completed ********************"); } catch (IOException e) {SyStem.              OUT.PRINTLN ("Decompression file failed");          E.printstacktrace ();                  } finally{try {in.close ();               Out.close ();                   } catch (Exception E2) {System. OUT.PRINTLN ("Managed IO stream failed");              E2.printstacktrace (); }}}/** * * @param sourcefilepath need to compress zip path * @param zipfilepath zip zipped file Path Path * @param fileName zip name * @return */public static Boolean filetozip (String sourcefilepath,string zi      Pfilepath,string fileName) {Boolean flag = false;      File SourceFile = new file (Sourcefilepath);      FileInputStream FIS = null;      Bufferedinputstream bis = null;      FileOutputStream fos = null;           Zipoutputstream Zos = null;      if (sourcefile.exists () = = False) {System. OUT.PRINTLN ("file directory to be compressed:" +sourcefilepath+ "does not exist.");       } else{File ZipFile = new file (Zipfilepath + "/" + FileName + ". zip");          try {if (zipfile.exists ()) {System. OUT.PRINTLN (Zipfilepath +) directory exists under the name: "+ FileName +". Z              IP "+" packaging files. ");                  } else{file[] sourceFiles = Sourcefile.listfiles (); if (null = = SourceFiles | | sourcefiles.length<1) {System. Out.println ("Files directory to be compressed:" + Sourcefilepath                  + "There is no file inside, no compression required.");                      } else{fos = new FileOutputStream (ZipFile);                      Zos = new Zipoutputstream (new Bufferedoutputstream (FOS));                      Zos.setencoding ("GBK");                      byte[] Bufs = new BYTE[1024*10]; for (int i=0;i<sourcefiles.length; i++) {//Create a zip entity and add a compressed package ZipEntry Zi                                                   Pentry = new ZipEntry (Sourcefiles[i].getname ());                          Zos.putnextentry (ZipEntry); Read the file to be compressed and write it into the compressed package FIS = new FileinpuTStream (Sourcefiles[i]);                          bis = new Bufferedinputstream (FIS, 1024*10);                          int read = 0;                          while ((Read=bis.read (bufs, 0, 1024*10))! =-1) {zos.write (bufs,0,read);                  }} flag = true;              }}} catch (FileNotFoundException e) {e.printstacktrace ();          throw new RuntimeException (e);              } catch (IOException e) {e.printstacktrace ();          throw new RuntimeException (e);                  } finally{//Close stream try {if (null! = bis) bis.close ();              if (null! = Zos) zos.close ();                  } catch (IOException e) {e.printstacktrace ();              throw new RuntimeException (e);  }}} return flag; }






Java to zip file contains Chinese sub-file operations

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.