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

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

 

转载于:https://www.cnblogs.com/simpledev/p/6852816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值