Java Function Code 7 -- Java creates zip and jar files

Source: Internet
Author: User
Tags crc32 create zip

7. Create zip and jar files in Java

If (ARGs. length <2) {<br/> system. err. println ("Usage: Java zipit zip.zip file1 file2 file3"); <br/> system. exit (-1); <br/>}< br/> file zipfile = new file (ARGs [0]); <br/> If (zipfile. exists () {<br/> system. err. println ("zip file already exists, please try another"); <br/> system. exit (-2); <br/>}< br/> fileoutputstream Fos = new fileoutputstream (zipfile); <br/> zipoutputstream ZOS = new zipoutputstream (fo S); <br/> int bytesread; <br/> byte [] buffer = new byte [1024]; <br/> CRC32 CRC = new CRC32 (); <br/> for (INT I = 1, n = args. length; I <n; I ++) {<br/> string name = ARGs [I]; <br/> file = new file (name ); <br/> If (! File. exists () {<br/> system. err. println ("Skipping:" + name); <br/> continue; <br/>}< br/> bufferedinputstream Bis = new bufferedinputstream (<br/> New fileinputstream (File); <br/> CRC. reset (); <br/> while (bytesread = bis. read (buffer ))! =-1) {<br/> CRC. update (buffer, 0, bytesread); <br/>}< br/> bis. close (); <br/> // reset to beginning of input stream <br/> Bis = new bufferedinputstream (New fileinputstream (File )); <br/> zipentry entry = new zipentry (name); <br/> entry. setmethod (zipentry. stored); <br/> entry. setcompressedsize (file. length (); <br/> entry. setsize (file. length (); <br/> entry. setcrc (CRC. getvalue (); <br/> ZOS. putnextentry (Entry ); <Br/> while (bytesread = bis. Read (buffer ))! =-1) {<br/> ZOS. write (buffer, 0, bytesread); <br/>}< br/> bis. close (); <br/>}< br/> ZOS. close ();

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.