在java中,主要是利用ZipEntry,ZipInputStream和ZipOutputStream來實現zip資料壓縮方式的編程方法,
| 構造方法摘要 |
ZipEntry(String name)
使用指定名稱建立新的 ZIP 條目。 |
| 構造方法摘要 |
ZipInputStream(InputStream in)
建立新的 ZIP 輸入資料流。 |
| 構造方法摘要 |
ZipOutputStream(OutputStream out)
建立新的 ZIP 輸出資料流。 |
ZipInputStream的主要方法
ZipEntry |
getNextEntry()
讀取下一個 ZIP 檔案條目並將流定位到該條目資料的開始處。 |
void |
closeEntry()
關閉當前 ZIP 條目並定位流以讀取下一個條目。 |
壓縮測試程式:
package com.book.ch09.file;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class CompressFile {public static void addFile(ZipOutputStream zipOutput, File file) throws IOException{System.out.println("正在添加檔案:" + file.getAbsolutePath());// 每個壓縮對象都為一個 ZipEntry 執行個體。ZipEntry zipEntry = new ZipEntry(file.getName());// 將這個執行個體放入壓縮檔中zipOutput.putNextEntry(zipEntry);// 讀取檔案內容FileInputStream ins = new FileInputStream(file);byte[] tmp = new byte[1024];int len = 0;while((len = ins.read(tmp)) != -1){zipOutput.write(tmp, 0, len);}ins.close();// 關閉當前的 ZipEntry 執行個體zipOutput.closeEntry();}public static void main(String[] args) throws IOException{File sourceFolder = new File("D:\\常用軟體");File targetFile = new File("D:\\zip_test.zip");// 使用 ZipOutputStream 建立壓縮檔ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream(targetFile));// 列出該檔案夾下所有的檔案File[] subFiles = sourceFolder.listFiles(new FileFilter(){public boolean accept(File pathname) {if(pathname.isFile())return true;return false;}});System.out.println("\r\n檔案壓縮開始:" +sourceFolder.getAbsolutePath()+"\n\n");for(int i=0; i<subFiles.length; i++){addFile(zipOutput, subFiles[i]);}zipOutput.close();System.out.println("\r\n檔案壓縮完成:" + targetFile.getAbsolutePath());}}
運行結果:
檔案壓縮開始:D:\常用軟體
正在添加檔案:D:\常用軟體\apache_2.2.4-win32-x86-no_ssl.zip
正在添加檔案:D:\常用軟體\CAJViewer7.0.zip
正在添加檔案:D:\常用軟體\CCleaner.rar
正在添加檔案:D:\常用軟體\CTeX_2.8.0.125.exe
正在添加檔案:D:\常用軟體\download
正在添加檔案:D:\常用軟體\DUBA2008_down_31_4306.exe
正在添加檔案:D:\常用軟體\DZH_INTERNET_V560w.exe
正在添加檔案:D:\常用軟體\EasyRecoveryPro-v6.20.rar
正在添加檔案:D:\常用軟體\FinalData-v2.01.rar
正在添加檔案:D:\常用軟體\FirefoxChinaEdition 2010.12.exe
正在添加檔案:D:\常用軟體\FirefoxChinaEdition-latest.exe
正在添加檔案:D:\常用軟體\fxalendar.exe
............
............
檔案壓縮完成:D:\zip_test.zip
解壓縮:
package com.book.ch09.file;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;public class UncompressFile {static int count = 0;public static void extractFile(File path, ZipInputStream zipIns, ZipEntry zipEntry) throws IOException{File file = new File(path, zipEntry.getName());System.out.println("正在釋放檔案:" + file.getAbsolutePath());FileOutputStream ous = new FileOutputStream(file);byte[] tmp = new byte[1024];int len = 0;while((len = zipIns.read(tmp)) != -1){ous.write(tmp, 0, len);}ous.close();zipIns.closeEntry();count++;}public static void main(String[] args) throws IOException{File sourceZipFile = new File("c:\\zip_test.zip");File targetFolder = new File("C:\\zip_test");targetFolder.mkdir();ZipInputStream zipIns = new ZipInputStream(new FileInputStream(sourceZipFile));ZipEntry zipEntry = zipIns.getNextEntry();while(zipEntry != null){extractFile(targetFolder, zipIns, zipEntry);zipEntry = zipIns.getNextEntry();}zipIns.close();System.out.println("\r\n檔案釋放完成。" + count + " 個檔案被釋放。");}}