package com.biao.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class TestZIP {
/**
* 功能:压缩多个文件成一个zip文件
* @param srcfile:源文件列表
* @param zipfile:压缩后的文件
*/
public static void zipFiles(File[] srcfile,File zipfile){
byte[] buf=new byte[1024];
try {
//ZipOutputStream类:完成文件或文件夹的压缩
ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipfile));
for(int i=0;i<srcfile.length;i++){
FileInputStream in=new FileInputStream(srcfile[i]);
out.putNextEntry(new ZipEntry(srcfile[i].getName()));
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
out.closeEntry();
in.close();
}
out.close();
System.out.println("压缩完成.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 功能:解压缩
* @param zipfile:需要解压缩的文件
* @param descDir:解压后的目标目录
*/
public static void unZipFiles(File zipfile,String descDir){
try {
ZipFile zf=new ZipFile(zipfile);
for(Enumeration entries=zf.entries();entries.hasMoreElements();){
ZipEntry entry=(ZipEntry) entries.nextElement();
String zipEntryName=entry.getName();
InputStream in=zf.getInputStream(entry);
OutputStream out=new FileOutputStream(descDir+zipEntryName);
byte[] buf1=new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
System.out.println("解压缩完成.");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**功能:
* @param args
*/
public static void main(String[] args) {
//2个源文件
File f1=new File("D:\\workspace\\flexTest\\src\\com\\biao\\test\\abc.txt");
File f2=new File("D:\\workspace\\flexTest\\src\\com\\biao\\test\\test.zip");
File[] srcfile={f1,f2};
//压缩后的文件
File zipfile=new File("D:\\workspace\\flexTest\\src\\com\\biao\\test\\biao.zip");
//TestZIP.zipFiles(srcfile, zipfile);
//需要解压缩的文件
File file=new File("D:\\workspace\\flexTest\\src\\com\\biao\\test\\biao.zip");
//解压后的目标目录
String dir="D:\\workspace\\flexTest\\src\\com\\biao\\test\\";
TestZIP.unZipFiles(file, dir);
}
}

Java实现多文件压缩打包的方法的更多相关文章

  1. Java /C# 实现文件压缩

    纯粹为了记录. 参考了 https://www.cnblogs.com/zeng1994/p/7862288.html import java.util.List; import java.util. ...

  2. Linux的文件的打包(tar方法)

    Linux的文件的打包(tar方法) tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一 ...

  3. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  4. java读取txt文件的2中方法---并将内容(每一行以固定的字符分割切成2段)存到map中去

    #java读取txt文件的第一种方法 /** * 方法:readTxt * 功能:读取txt文件并把txt文件的内容---每一行作为一个字符串加入到List中去 * 参数:txt文件的地址 * 返回: ...

  5. Java中移动文件或目录的方法盘点

    本文不再更新,可能存在内容过时的情况,实时更新请移步原文地址:Java中移动文件或目录的方法盘点: import org.apache.commons.io.FileUtils; import jav ...

  6. Java 多个文件压缩下载

    有时候会有多个附件一起下载的需求,这个时候最好就是打包下载了 首先下面这段代码是正常的单个下载 public void Download(@RequestParam("file_path&q ...

  7. java实现将文件压缩成zip格式

    以下是将文件压缩成zip格式的工具类(复制后可以直接使用): zip4j.jar包下载地址:http://www.lingala.net/zip4j/download.php package util ...

  8. Linux文件压缩/打包/解压

    在Linux日常维护中,经常需要备份同步一些比较重要的文件,而在传输过程中如果文件比较大往往会非常慢,而且还会非常占用空间,这时候就需要我们使用压缩工具对大文件进行压缩打包,下面我们来介绍一下常用的压 ...

  9. Java读取Excel文件的几种方法

    Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...

随机推荐

  1. C# 泛型特化

    C# 泛型不是 C++ 的模板类,并不支持特化和偏特化,但是使用一些技巧可以在一定程度上达到相同的目的. 原文是 po 在 stackoverflow 上的一个回答:A: Generic indexe ...

  2. amazeui学习笔记--js插件(UI增强3)--折叠面板Collapse

    amazeui学习笔记--js插件(UI增强3)--折叠面板Collapse 一.总结 注意点: 1.data-am-collapse:这个东西就是展开折叠事件 2.am-collapse(包括其下属 ...

  3. mootools常用特性和示例(基础篇1)

    网上关于mootools这个库的信息很少. 公司一些老的项目用到了mootools库,因为要维护,所以接触到了mootools. mootools(文档)官网:http://www.chinamoot ...

  4. UVA 10047 - The Monocycle BFS

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. mysql :Native table 'performance_schema'.'cond_instances' has the wrong structure

    err: 150418 13:25:06 [ERROR] Native table 'performance_schema'.'cond_instances' has the wrong struct ...

  6. 8.5 Android灯光系统_源码分析_通知灯

    参考文章(应用程序举例)how to use the LED with Android phonehttp://androidblogger.blogspot.jp/2009/09/tutorial- ...

  7. ios越狱开发

    theos/Logos常用命令 %hook 用的最多,意思是钩住一个类. %hook SpringBoard %end %new (v@:) 新建方法 v是返回值@代表参数名 %new(v@:@i) ...

  8. Linux动态链接库的创建与使用

    Linux动态链接库的创建与使用1. 介绍     使用GNU的工具我们如何在Linux下创建自己的程序函数库?一个“程序函数库”简单的说就是一个文件包含了一些编译好的代码和数据,这些编译好的代码和数 ...

  9. VS_VERSION_INFO

    VS_VERSION_INFO这里可以修改ocx的版本号

  10. [PReact] Use Link State to Automatically Handle State Changes

    Storing and updating values inside a component’s local state (known as controlled components) is suc ...