`

操作Zip压缩文件_Zip2Utils

阅读更多
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;


public class Zip2Utils {

	private static final int BUF_SIZE = 1024 * 1024;
	
	public static void main(String[] args){
		Collection<File> resFileList = new ArrayList<File>();
		
		resFileList.add( new File("d://temp//1.txt"));
		resFileList.add( new File("d://temp//2.txt"));
		File file3 = new File("d://temp//test.zip");
		
		try {
			//压缩文件
			Zip2Utils.zipFiles(resFileList, file3, "this is test");
			//解压缩文件
			Zip2Utils.upZipFile(file3, "d://ddd");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void zipFiles(Collection<File> resFileList , File zipFile ,String comment) throws IOException{
		ZipOutputStream out  = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile),BUF_SIZE));
		for(File file:resFileList){
			Zip2Utils.zipFile(file, out, "");
		}
		out.close(); 
	}
	
	public static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws IOException{
		rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) + resFile.getName();    
        System.out.println(rootpath);
		if (resFile.isDirectory()) {    
                File[] fileList = resFile.listFiles();    
                for (File file : fileList) {    
                        zipFile(file, zipout, rootpath);    
                }    
        } else {    
                byte buffer[] = new byte[BUF_SIZE];    
                BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), BUF_SIZE);    
                zipout.putNextEntry(new ZipEntry(rootpath));    
                int realLength;    
                while ((realLength = in.read(buffer)) != -1) {    
                        zipout.write(buffer, 0, realLength);    
                }    
                in.close();    
                zipout.flush();    
                zipout.closeEntry();    
        }    	
	}
	/**   
     * 解压缩一个文件   
     *   
     * @param zipFile        压缩文件   
     * @param folderPath 解压缩的目标目录   
     * @throws IOException 当压缩过程出错时抛出   
     */    
    public static void upZipFile(File zipFile, String folderPath) throws IOException {    
    		File targetFolder = new File(folderPath);
    		if(!targetFolder.exists()){
    			targetFolder.mkdir();
    		}
            ZipFile zf = new ZipFile(zipFile);    
            for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {    
                    ZipEntry entry = ((ZipEntry) entries.nextElement());    
                    InputStream in = zf.getInputStream(entry);    
                    OutputStream out = new FileOutputStream(folderPath + File.separator + entry.getName());    
                    byte buffer[] = new byte[BUF_SIZE];    
                    int realLength;    
                    while ((realLength = in.read(buffer)) > 0) {    
                            out.write(buffer, 0, realLength);    
                    }    
                    in.close();    
                    out.close();    
            }    
    }    

}

 

分享到:
评论

相关推荐

    zip_utils_src

    zip_utils_src c++关于压缩成zip文件的源码与实例!

    zip_utils_src.zip

    zip util 缩解压缩源码以及 VS2015 示例... https://www.codeproject.com/Articles/7530/Zip-Utils-Clean-Elegant-Simple-Cplusplus-Win

    zip_utils_src.rar

    zip的压缩与解压程序 zip.h zip.cpp unzip.h unzip.cpp 文件来源:http://www.codeproject.com/Articles/7530/Zip-Utils-clean-elegant-simple-C-Win

    zip_utils_century4gy_C++ZipUtils压缩_Vc_zip_

    基于c++ mfc 解压 压缩源码,包括了以下相关类:zip.h、zip.cpp、unzip.h、unzip.cpp文件

    c++ mfc 解压 压缩 源码 zip_utils_src

    c++ mfc 解压 压缩 zip

    压缩库代码

    zip_utils_src(2).zip

    java压缩文件源码--ZipUtils

    // 设置压缩文件入口entry,为被读取的文件创建压缩条目 File tempFile = new File(fileArray[i].toString()); String rootStr = file.getPath(); String entryStr = null; // entry以相对路径的...

    plexus-utils-2.0.5.jar.zip

    压缩,使文件变小,与ZIP压缩机制完全相同。 包封装。能够让JAR包里面的文件依赖于统一版本的类文件。 可移植性,能够在各种平台上直接使用。 把一个JAR文件添加到系统的classpath环境变量之后,java通常会把这个...

    plexus-utils-3.0.jar.zip

    压缩,使文件变小,与ZIP压缩机制完全相同。 包封装。能够让JAR包里面的文件依赖于统一版本的类文件。 可移植性,能够在各种平台上直接使用。 把一个JAR文件添加到系统的classpath环境变量之后,java通常会把这个...

    plexus-utils-1.5.1.jar.zip

    压缩,使文件变小,与ZIP压缩机制完全相同。 包封装。能够让JAR包里面的文件依赖于统一版本的类文件。 可移植性,能够在各种平台上直接使用。 把一个JAR文件添加到系统的classpath环境变量之后,java通常会把这个...

    vc6 ZIP 操作模块

    源码采用 Zip Utils,原版不支持vc6.0,经过修改以后,已经完美支持vc6.0编译,本例子中包含测试模块!

    ZipUtils.cs

    只支持Zip的解压缩,

    windows zip unzip

    Zip Utils--简单优雅的C++接口 挺好用的一个封装文件,可以实现压缩,解压,遍历压缩文件等操作。

    ZipUtils.zip

    由于项目的需要使用到文档的压缩和解压功能,期初打算使用zlib,发现其只是提供了底层的接口供使用,主要是用来对文字的压缩...后来发现了zip utils的包,现再其基础上进行修改,目前满足自身对文件解压和压缩的使用。

    javautils:Java实用程序

    zip,gzip等文件压缩工具(待补充) 5 com.wind.office excel读写工具类(待补充) 6 com.wind.network http请求工具类,android均可使用(待补充) 7 com.wind.xml xml解析工具类 8 com.wind.jdbc java jdbc工具类, ...

    Android Zip解压缩工具类分享

    本文实例为大家分享了Android Zip解压缩工具类的具体代码,供大家参考,具体内容如下 package com.example.zip.commons.utils; import android.util.Log; import java.io.File; import java.io.FileInputStream; ...

    centOS7安装nginx安装包以及所有依赖包.zip

    CentOS7+,压缩保证包含 gcc\gcc-c++所有安装rpm包;openssl包;zlib包;pcre的包以及nginx安装包,支持Linux下离线安装nginx 如下: nginx-1.15.12.tar.gz openssl-1.0.2r.tar.gz pcre-8.42.tar.gz zlib-1.2.11.tar...

    tile-utils:Python脚本,用于拼接来自多个来源的平铺地图(必应航拍图,网络地图,MBTiles文件)-python source file

    并解压缩ZIP存档 用Bing Maps Key替换bing_maps_key.txt的内容。 请参阅了解如何获取“必应地图”密钥 用法 看 cd pathTo/tile-utils python stitch.py -h 重要的提示: 如果您的边界框定义以减号开头,请在其前面...

Global site tag (gtag.js) - Google Analytics