1. 程式人生 > >Java 使用ZipOutputStream 進行打包操作

Java 使用ZipOutputStream 進行打包操作

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

打包單個檔案:

public static void main(String[] args) throws IOException{        File file = new
File("d:" + File.separator + "aaa.java");        File zipFile = new File("d:" + File.separator + "hello.zip");        InputStream input = new FileInputStream(file);        ZipOutputStream zipOut = new
ZipOutputStream(new FileOutputStream(                zipFile));        zipOut.putNextEntry(new ZipEntry(file.getName()));        // 設定註釋        zipOut.setComment("hello"
);        int temp = 0;        while((temp = input.read()) != -1){            zipOut.write(temp);        }        input.close();        zipOut.close();    }


打包多個檔案:

public static void main(String[] args) throws IOException{        // 要被壓縮的資料夾        File file = new File("d:" + File.separator + "temp");        File zipFile = new File("d:" + File.separator + "zipFile.zip");        InputStream input = null;        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(                zipFile));        zipOut.setComment("hello");        if(file.isDirectory()){            File[] files = file.listFiles();            for(int i = 0; i < files.length; ++i){                input = new FileInputStream(files[i]);                zipOut.putNextEntry(new ZipEntry(file.getName()                        + File.separator + files[i].getName()));                int temp = 0;                while((temp = input.read()) != -1){                    zipOut.write(temp);                }                input.close();            }        }        zipOut.close();    }

大家自然想到,既然能壓縮,自然能解壓縮,在談解壓縮之前,我們會用到一個ZipFile類,先給一個這個例子吧。java中的每一個壓縮檔案都是可以使用ZipFile來進行表示的

import java.io.File;import java.io.IOException;import java.util.zip.ZipFile; /** * ZipFile演示 * */public class ZipFileDemo{    public static void main(String[] args) throws IOException{        File file = new File("d:" + File.separator + "hello.zip");        ZipFile zipFile = new ZipFile(file);        System.out.println("壓縮檔案的名稱為:" + zipFile.getName());    }}

解壓單個檔案:

 public static void main(String[] args) throws IOException{        File file = new File("d:" + File.separator + "hello.zip");        File outFile = new File("d:" + File.separator + "unZipFile.txt");        ZipFile zipFile = new ZipFile(file);        ZipEntry entry = zipFile.getEntry("hello.txt");//hello.txt 為壓縮包中檔案的名稱        InputStream input = zipFile.getInputStream(entry);        OutputStream output = new FileOutputStream(outFile);        int temp = 0;        while((temp = input.read()) != -1){            output.write(temp);        }        input.close();        output.close();    }

解壓多個檔案:

當我們需要解壓縮多個檔案的時候,ZipEntry就無法使用了,如果想操作更加複雜的壓縮檔案,我們就必須使用ZipInputStream類

public static void main(String[] args) throws IOException{        File file = new File("d:" + File.separator + "zipFile.zip");        File outFile = null;        ZipFile zipFile = new ZipFile(file);        ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));        ZipEntry entry = null;        InputStream input = null;        OutputStream output = null;        while((entry = zipInput.getNextEntry()) != null){            System.out.println("解壓縮" + entry.getName() + "檔案");//entry.getName()獲得壓縮包內的檔案及檔案路徑(aaaa/bb.txt)            outFile = new File("d:" + File.separator + entry.getName());            if(!outFile.getParentFile().exists()){                outFile.getParentFile().mkdir();            }            if(!outFile.exists()){                outFile.createNewFile();            }            input = zipFile.getInputStream(entry);            output = new FileOutputStream(outFile);            int temp = 0;            while((temp = input.read()) != -1){                output.write(temp);            }            input.close();            output.close();        }    }



           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述