java實現 zip解壓縮

來源:互聯網
上載者:User

標籤:定義   java   res   col   not   port   pfile   null   bin   

程式實現了ZIP壓縮。共分為2部分 : 壓縮(compression)與解壓(decompression)

大致功能包括用了多態,遞迴等JAVA核心技術,可以對單個檔案和任意級聯檔案夾進行壓縮和解壓。 需在代碼中自訂來源輸入路徑和目標輸出路徑。 

1.    package com.han;  2.      3.    import java.io.*;  4.    import java.util.zip.*;  5.      6.    /** 7.     * 程式實現了ZIP壓縮。共分為2部分 : 壓縮(compression)與解壓(decompression) 8.     * <p> 9.     * 大致功能包括用了多態,遞迴等JAVA核心技術,可以對單個檔案和任意級聯檔案夾進行壓縮和解壓。 需在代碼中自訂來源輸入路徑和目標輸出路徑。 10.     * <p> 11.     * 在本段代碼中,實現的是壓縮部分;解壓部分見本包中Decompression部分。 12.     *  13.     * @author HAN 14.     *  15.     */  16.      17.    public class MyZipCompressing {  18.        private int k = 1; // 定義遞迴次數變數  19.      20.        public MyZipCompressing() {  21.            // TODO Auto-generated constructor stub  22.        }  23.      24.        /** 25.         * @param args 26.         */  27.        public static void main(String[] args) {  28.            // TODO Auto-generated method stub  29.            MyZipCompressing book = new MyZipCompressing();  30.            try {  31.                book.zip("C:\\Users\\Gaowen\\Desktop\\ZipTestCompressing.zip",  32.                        new File("C:\\Users\\Gaowen\\Documents\\Tencent Files"));  33.            } catch (Exception e) {  34.                // TODO Auto-generated catch block  35.                e.printStackTrace();  36.            }  37.      38.        }  39.      40.        private void zip(String zipFileName, File inputFile) throws Exception {  41.            System.out.println("壓縮中...");  42.            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(  43.                    zipFileName));  44.            BufferedOutputStream bo = new BufferedOutputStream(out);  45.            zip(out, inputFile, inputFile.getName(), bo);  46.            bo.close();  47.            out.close(); // 輸出資料流關閉  48.            System.out.println("壓縮完成");  49.        }  50.      51.        private void zip(ZipOutputStream out, File f, String base,  52.                BufferedOutputStream bo) throws Exception { // 方法重載  53.            if (f.isDirectory()) {  54.                File[] fl = f.listFiles();  55.                if (fl.length == 0) {  56.                    out.putNextEntry(new ZipEntry(base + "/")); // 建立zip壓縮排入點base  57.                    System.out.println(base + "/");  58.                }  59.                for (int i = 0; i < fl.length; i++) {  60.                    zip(out, fl[i], base + "/" + fl[i].getName(), bo); // 遞迴遍曆子檔案夾  61.                }  62.                System.out.println("第" + k + "次遞迴");  63.                k++;  64.            } else {  65.                out.putNextEntry(new ZipEntry(base)); // 建立zip壓縮排入點base  66.                System.out.println(base);  67.                FileInputStream in = new FileInputStream(f);  68.                BufferedInputStream bi = new BufferedInputStream(in);  69.                int b;  70.                while ((b = bi.read()) != -1) {  71.                    bo.write(b); // 將位元組流寫入當前zip目錄  72.                }  73.                bi.close();  74.                in.close(); // 輸入資料流關閉  75.            }  76.        }  77.    }  
1.    package com.han;  2.      3.    import java.io.*;  4.    import java.util.zip.*;  5.    /** 6.     * 程式實現了ZIP壓縮。共分為2部分 : 7.     * 壓縮(compression)與解壓(decompression) 8.     * <p> 9.     * 大致功能包括用了多態,遞迴等JAVA核心技術,可以對單個檔案和任意級聯檔案夾進行壓縮和解壓。 10.     * 需在代碼中自訂來源輸入路徑和目標輸出路徑。 11.     * <p> 12.     * 在本段代碼中,實現的是解壓部分;壓縮部分見本包中compression部分。 13.     * @author HAN 14.     * 15.     */  16.    public class CopyOfMyzipDecompressing {  17.          18.        public static void main(String[] args) {  19.            // TODO Auto-generated method stub  20.            long startTime=System.currentTimeMillis();  21.            try {  22.                ZipInputStream Zin=new ZipInputStream(new FileInputStream(  23.                        "C:\\Users\\HAN\\Desktop\\stock\\SpectreCompressed.zip"));//輸入源zip路徑  24.                BufferedInputStream Bin=new BufferedInputStream(Zin);  25.                String Parent="C:\\Users\\HAN\\Desktop"; //輸出路徑(檔案夾目錄)  26.                File Fout=null;  27.                ZipEntry entry;  28.                try {  29.                    while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){  30.                        Fout=new File(Parent,entry.getName());  31.                        if(!Fout.exists()){  32.                            (new File(Fout.getParent())).mkdirs();  33.                        }  34.                        FileOutputStream out=new FileOutputStream(Fout);  35.                        BufferedOutputStream Bout=new BufferedOutputStream(out);  36.                        int b;  37.                        while((b=Bin.read())!=-1){  38.                            Bout.write(b);  39.                        }  40.                        Bout.close();  41.                        out.close();  42.                        System.out.println(Fout+"解壓成功");      43.                    }  44.                    Bin.close();  45.                    Zin.close();  46.                } catch (IOException e) {  47.                    // TODO Auto-generated catch block  48.                    e.printStackTrace();  49.                }  50.            } catch (FileNotFoundException e) {  51.                // TODO Auto-generated catch block  52.                e.printStackTrace();  53.            }  54.            long endTime=System.currentTimeMillis();  55.            System.out.println("耗費時間: "+(endTime-startTime)+" ms");  56.        }  57.      58.    }  

 

java實現 zip解壓縮

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.