博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java生成zip压缩文件,解压缩文件
阅读量:7108 次
发布时间:2019-06-28

本文共 4152 字,大约阅读时间需要 13 分钟。

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

 

转载地址:http://wvvhl.baihongyu.com/

你可能感兴趣的文章
深入Java虚拟机之虚拟机体系结构
查看>>
Bitcoin的解决的一个核心问题是什么
查看>>
java NIO2(file io)
查看>>
【读书笔记】06 | 白话容器基础(二):隔离与限制
查看>>
Django 学习笔记(二)
查看>>
Linux系统的grep以及正则表达式浅析!
查看>>
Orange的扩展插件Widgets开发(七)-GUI Control
查看>>
用python实现银行转账功能
查看>>
python学习笔记---列表
查看>>
Decorator 装饰器小记
查看>>
Centos6.5搭建Wiki
查看>>
linux 清空文件内容的方法
查看>>
2018“硅谷技划”随笔(三):人工智能火热背后的真正温度
查看>>
综合技术 --ORM
查看>>
我的友情链接
查看>>
[C++]STL萃取学习
查看>>
httpClient学习
查看>>
函数指针和指针函数的区别
查看>>
贪吃蛇小游戏
查看>>
搭建Mycat实现读写分离
查看>>