分享非常有用的Java程式 (關鍵代碼) (三)---建立ZIP和JAR檔案

來源:互聯網
上載者:User

標籤:

原文: 分享非常有用的Java程式 (關鍵代碼) (三)---建立ZIP和JAR檔案

 

import java.util.zip.*;   
import java.io.*;       public class ZipIt {       public static void main(String args[]) throws IOException { if (args.length < 2) {              System.err.println("usage: java ZipIt Zip.zip file1 file2 file3");               System.exit(-1);           }           File zipFile = new File(args[0]);           if (zipFile.exists()) {               System.err.println("Zip file already exists, please try another");System.exit(-2);          }           FileOutputStream fos = new FileOutputStream(zipFile);           ZipOutputStream zos = new ZipOutputStream(fos);           int bytesRead;           byte[] buffer = new byte[1024];           CRC32 crc = new CRC32();           for (int i=1, n=args.length; i < n; i++) {               String name = args[i];               File file = new File(name);               if (!file.exists()) {                   System.err.println("Skipping: " + name);                   continue;               }               BufferedInputStream bis = new BufferedInputStream( new FileInputStream(file));               crc.reset();               while ((bytesRead = bis.read(buffer)) != -1) {                   crc.update(buffer, 0, bytesRead);               }               bis.close();               // Reset to beginning of input stream               bis = new BufferedInputStream(new FileInputStream(file));               ZipEntry entry = new ZipEntry(name);               entry.setMethod(ZipEntry.STORED);              entry.setCompressedSize(file.length());               entry.setSize(file.length());               entry.setCrc(crc.getValue());               zos.putNextEntry(entry);              while ((bytesRead = bis.read(buffer)) != -1) {                   zos.write(buffer, 0, bytesRead);              }               bis.close();           }           zos.close();       }   } }}}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

分享非常有用的Java程式 (關鍵代碼) (三)---建立ZIP和JAR檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.