1. 程式人生 > >java生成zip壓縮文件,解壓縮文件

java生成zip壓縮文件,解壓縮文件

fileinput 重復 temp parent mp4 define blog exc java

1.生成zip

public static void main(String[] args) {
        try {
//            testZip("c:\\temp.txt", "c:\\temp4.zip");
//            testZip("c:\\Result.txt", "c:\\temp4.zip");  //不然會被一個文件覆蓋了.
            
            //壓縮多個文件的關鍵: ZipOutputStream out 作為參數傳遞.
            //一個流,否則存在覆蓋的問題,即每次會new一個,所以外置.
            ZipOutputStream zos = null
; zos = new ZipOutputStream(new FileOutputStream("c:\\temp5.zip")); testZip("c:\\temp.txt",zos); testZip("c:\\Result.txt",zos); //外置 zos.closeEntry(); zos.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * 重復壓縮文件 * @param zipEntryName // 去掉String zipEntryName, * @param filePath * @param zipPath * @throws Exception */ public static void testZip(String filePath,ZipOutputStream zos) throws Exception{ //壓縮包 /*ZipOutputStream zos = null;*/ //
BufferedOutputStream bos = null; // File zipFile = new File(zipPath); // if(zipFile.exists()==false){ /* zos = new ZipOutputStream(new FileOutputStream(zipPath));*/ // bos = new BufferedOutputStream(zos); //Buffer... // }else{ // // } File f = new File(filePath); // //create zip FileInputStream fis = new FileInputStream(f); // BufferedInputStream bis = new BufferedInputStream(fis); // set the file name in the .zip file // zos.putNextEntry(new ZipEntry(zipEntryName)); zos.putNextEntry(new ZipEntry(f.getName())); // set the declear zos.setComment("by zip test!"); // byte[] b = new byte[1024]; // while (true) { // int len = bis.read(b); // if (len == -1) // break; // bos.write(b, 0, len); // System.out.println(new String(b, 0, len)); // } // bos.flush(); //這一行重要,否則txt是空白文件. byte[] buffer = new byte[1024]; int len = 0 ; // 讀取文件的內容,打包到zip文件 while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } fis.close(); /*zos.closeEntry(); zos.close();*/ }

2.解壓縮

public static void main(String[] args) throws Exception {
        // get a zip file instance
        File file = new File("c:\\temp5.zip");

        // get a ZipFile instance
        ZipFile zipFile = new ZipFile(file);

        // create a ZipInputStream instance
        ZipInputStream zis = new ZipInputStream(new FileInputStream(file));

        // create a ZipEntry instance , lay the every file from
        // decompress file temporarily
        ZipEntry entry = null;

        // a circle to get every file
        while ((entry = zis.getNextEntry()) != null) {
            System.out.println("decompress file :" + entry.getName());

            // define the path to set the file
            File outFile = new File("c:\\zip\\"
                    + entry.getName());

            // if the file‘s parent directory wasn‘t exits ,than
            // create the directory
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdir();
            }

            // if the file not exits ,than create the file
            if (!outFile.exists()) {
                outFile.createNewFile();
            }

            // create an input stream  讀文件
            BufferedInputStream bis = new BufferedInputStream(
                    zipFile.getInputStream(entry));

            // create an output stream 寫文件
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(outFile));
            byte[] b = new byte[1024];
            while (true) {
                int len = bis.read(b);
                if (len == -1)
                    break;
                bos.write(b, 0, len);
            }
            // close stream
            bis.close();
            bos.close();
        }
        zis.close();
    }

java生成zip壓縮文件,解壓縮文件