Java GZIPOutputStream流壓縮文件的操作
我就廢話不多說了,大家還是直接看代碼吧~
不多說,直接上代碼
public static void main(String[] args) throws Exception{//壓縮文件 File src = new File('e:/xx/aa.txt'); File zipFile = new File('e:/xx/a.zip'); FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); ZipEntry entry = new ZipEntry( src.getName()); zos.putNextEntry(entry); int count; byte[] buf = new byte[1024]; while ((count = bis.read(buf)) != -1) { zos.write(buf, 0, count); } bis.close(); //fos.close(); zos.close();// }壓縮的步驟是:
src將要壓縮的文件,zipFile 壓縮后的文件,壓縮流套接zipFile,然后將src文件寫入zipFile,其中ZipEntry中放入的源文件的當(dāng)前名稱,putNextEntry是將源文件的當(dāng)前名稱定位到條目數(shù)據(jù)的開始處。
補(bǔ)充:Java ZIP壓縮輸入輸出流
ZIP是一種較為常見的壓縮形式,在Java中要想實(shí)現(xiàn)ZIP的壓縮需要導(dǎo)入java.util.zip包,可以使用此包中的ZipFile、ZipOutputStream、ZipInputStream、ZipEntry幾個(gè)類完成。
ZipOutputStream類的常用方法
ZipInputStream類的常用方法
在JAVA IO中,不僅可以實(shí)現(xiàn)ZIP壓縮格式的輸入、輸出,也可以實(shí)現(xiàn)JAR及GZIP文件格式的壓縮:
1、JAR壓縮的支持類保存在java.util.jar包中,常用的類有 JarOutputStream(JAR壓縮輸出流)、JarInputStream(JAR壓縮輸入流)、JARFile(JAR文件)、JAREntry(JAR實(shí)體)
2、GZIP是用于UNIX系統(tǒng)的文件壓縮,在Linux中經(jīng)常會(huì)使用到*.gz的文件,就是GZIP格式,GZIP壓縮的支持類保存在java.util.zip包中,常用的類有 GZIPOutputStream(GZIP壓縮輸出流)、GZIPInputStream(GZIP壓縮輸入流)
注意:
1、壓縮文件中的每一個(gè)壓縮實(shí)體都使用ZipEntry保存,一個(gè)壓縮文件中可能包含一個(gè)或多個(gè)ZipEntry對(duì)象。
2、在JAVA中可以進(jìn)行zip、jar、gz三種格式的壓縮支持,操作流程基本上是一致的。
3、ZipOutputStream可以進(jìn)行壓縮的輸出,但是輸出的位置不一定是文件。
4、ZipFile表示每一個(gè)壓縮文件,可以得到每一個(gè)壓縮實(shí)體的輸入流。
壓縮文件import java.io.*;import java.util.zip.*; public class MyZip { // 創(chuàng)建類 private void zip(String zipFileName, File inputFile) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipFileName)); // 創(chuàng)建ZipOutputStream類對(duì)象 zip(out, inputFile, ''); // 調(diào)用方法 System.out.println('壓縮中…'); // 輸出信息 out.close(); // 將流關(guān)閉 } private void zip(ZipOutputStream out, File f, String base) throws Exception { // 方法重載 if (f.isDirectory()) { // 測(cè)試此抽象路徑名表示的文件是否是一個(gè)目錄 File[] fl = f.listFiles(); // 獲取路徑數(shù)組 out.putNextEntry(new ZipEntry(base + '/')); // 寫入此目錄的entry base = base.length() == 0 ? '' : base + '/'; // 判斷參數(shù)是否為空 for (int i = 0; i < fl.length; i++) { // 循環(huán)遍歷數(shù)組中文件 zip(out, fl[i], base + fl[i]); } } else { out.putNextEntry(new ZipEntry(base)); // 創(chuàng)建新的進(jìn)入點(diǎn) // 創(chuàng)建FileInputStream對(duì)象 FileInputStream in = new FileInputStream(f); int b; // 定義int型變量 System.out.println(base); while ((b = in.read()) != -1) { // 如果沒有到達(dá)流的尾部 out.write(b); // 將字節(jié)寫入當(dāng)前ZIP條目 } in.close(); // 關(guān)閉流 } } public static void main(String[] temp) { // 主方法 MyZip book = new MyZip(); // 創(chuàng)建本例對(duì)象 try { // 調(diào)用方法,參數(shù)為壓縮后文件與要壓縮文件 book.zip('hello.zip', new File('src')); System.out.println('壓縮完成'); // 輸出信息 } catch (Exception ex) { ex.printStackTrace(); } }}解壓文件
import java.io.*;import java.util.zip.*; public class Decompressing { // 創(chuàng)建文件 public static void main(String[] temp) { ZipInputStream zin; // 創(chuàng)建ZipInputStream對(duì)象 try { // try語句捕獲可能發(fā)生的異常 zin = new ZipInputStream(new FileInputStream('hello.zip')); // 實(shí)例化對(duì)象,指明要進(jìn)行解壓的文件 ZipEntry entry = zin.getNextEntry(); // 獲取下一個(gè)ZipEntry while (((entry = zin.getNextEntry()) != null) && !entry.isDirectory()) { // 如果entry不為空,并不在同一目錄下 File file = new File('d:' + entry.getName()); // 獲取文件目錄 System.out.println(file); if (!file.exists()) { // 如果該文件不存在 file.mkdirs();// 創(chuàng)建文件所在文件夾 file.createNewFile(); // 創(chuàng)建文件 } zin.closeEntry(); // 關(guān)閉當(dāng)前entry System.out.println(entry.getName() + '解壓成功'); } zin.close(); // 關(guān)閉流 } catch (Exception e) { e.printStackTrace(); } }}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程2. Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼3. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式4. 一個(gè) 2 年 Android 開發(fā)者的 18 條忠告5. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼6. Spring的異常重試框架Spring Retry簡(jiǎn)單配置操作7. Android studio 解決logcat無過濾工具欄的操作8. 什么是Python變量作用域9. PHP正則表達(dá)式函數(shù)preg_replace用法實(shí)例分析10. vue-drag-chart 拖動(dòng)/縮放圖表組件的實(shí)例代碼
