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();
    }
时间: 2024-08-08 13:50:57

java生成zip压缩文件,解压缩文件的相关文章

java.util.zip包在解压缩文件方面入门使用

工作空闲之余,无意间点了一下java.util.zip包下面的一些源码看了一看.知道其使用的范围就是针对我们日常中关于 文件的压缩和解压的操作.写了一些常用的压缩文件(单个文件和多个文件的压缩).解压文件(解压单个文件和文件夹)的 小小Demo. package com.clark.zip; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IO

Java.util.zip 压缩与解压缩工具类

Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jdk 编写该工具类,不依赖任何第三方 jar,随用随取,实现功能大体如下: 1.目录级别递归压缩与解压缩 zip: 2.单文件压缩和解压缩 zip : import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipIn

java.util.zip压缩打包文件总结一:压缩文件及文件下面的文件夹

一.简述 zip用于压缩和解压文件.使用到的类有:ZipEntry  ZipOutputStream 二.具体实现代码 package com.joyplus.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.

Java使用Zip压缩文件或整个目录

1.压缩文件或整个目录 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.zip.ZipEntry

ASP.NET打包生成zip压缩文件

using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using Microsoft.Win32; using

java直接生成zip压缩文件精简代码(跳过txt文件)

/** * @param args */ public static void main(String[] args) throws Exception{ ZipOutputStream zos = null; zos = new ZipOutputStream(new FileOutputStream("c:\\temp7.zip")); String ss = "cName\t\tcCode\t\tcreateDate\n"; ss += "dbc券9

java.util.zip压缩打包文件总结二: ZIP解压技术

一.简述 解压技术和压缩技术正好相反,解压技术要用到的类:由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码,如: CheckedInputStream cis = new CheckedInputStream(new FileInputStream( srcFile), new CRC32()); ZipInputStream zis = new ZipInputStream(cis); 需要注意的是,在构建解压文件时,需要

php生成zip压缩文件的方法,支持文件和压缩包路径查找

/* * new creatZip($_dir,$_zipName); *@ _dir是被压缩的文件夹名称,可使用路径,例 'a'或者'a/test.txt'或者'test.txt' *@ _zipName是压缩后的压缩包名称,可使用路径,例 'a/test.zip'或者'test.zip' **/ class creatZip { private $_dir; private $_zipDir; private $_zipName; public function __construct($d

java实现zip压缩文件

在网上找到这几种方法,感觉不错,收集起来,方便大家使用 jdk自带的包java.util.zip.ZipOutputStream,不足之处,文件(夹)名称带中文时,出现乱码问题,实现代码如下 /** * 功能:把 sourceDir 目录下的所有文件进行 zip 格式的压缩,保存为指定 zip 文件 * @param sourceDir 如果是目录,eg:D:\\MyEclipse\\first\\testFile,则压缩目录下所有文件: * 如果是文件,eg:D:\\MyEclipse\\fi