java生成zip压缩文件,解压缩文件

时间:2022-01-15 11:51:02

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();
    }