Java实现压缩文件与解压缩文件
由于工作需要,需要将zip的压缩文件进行解压,经过调查发现,存在两个开源的工具包,一个是Apache的ant工具包,另一个就是Java api自带的工具包;但是Java自带的工具包存在问题:如果压缩或者解压的文件存在非英文字符(比如中文、以色列文),在操作的过程中会存在问题:MALFORMAL Eception……
以下是通过Apache的zip工具包进行压缩和解压的代码(需要ant.jar):
package com.steven.file; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List; import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream; /**
* 压缩或解压zip:
* 由于直接使用java.util.zip工具包下的类,会出现中文乱码问题,所以使用ant.jar中的org.apache.tools.zip下的工具类
* @author Administrator
*/ public class ZipUtil { /**
* 压缩文件或路径
* @param zip 压缩的目的地址
* @param srcFiles 压缩的源文件
*/
public static void zipFile( String zip , List<File> srcFiles ){
try {
if( zip.endsWith(".zip") || zip.endsWith(".ZIP") ){
ZipOutputStream _zipOut = new ZipOutputStream(new FileOutputStream(new File(zip))) ;
_zipOut.setEncoding("GBK");
for( File _f : srcFiles ){
handlerFile(zip , _zipOut , _f , "");
}
_zipOut.close();
}else{
System.out.println("target file[" + zip + "] is not .zip type file");
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
} /**
*
* @param zip 压缩的目的地址
* @param zipOut
* @param srcFile 被压缩的文件信息
* @param path 在zip中的相对路径
* @throws IOException
*/
private static void handlerFile(String zip , ZipOutputStream zipOut , File srcFile , String path ) throws IOException{
System.out.println(" begin to compression file[" + srcFile.getName() + "]");
if( !"".equals(path) && ! path.endsWith(File.separator)){
path += File.separator ;
}
if( ! srcFile.getPath().equals(zip) ){
if( srcFile.isDirectory() ){
File[] _files = srcFile.listFiles() ;
if( _files.length == 0 ){
zipOut.putNextEntry(new ZipEntry( path + srcFile.getName() + File.separator));
zipOut.closeEntry();
}else{
for( File _f : _files ){
handlerFile( zip ,zipOut , _f , path + srcFile.getName() );
}
}
}else{
byte[] _byte = new byte[1024] ;
InputStream _in = new FileInputStream(srcFile) ;
zipOut.putNextEntry(new ZipEntry(path + srcFile.getName()));
int len = 0 ;
while( (len = _in.read(_byte)) > 0 ){
zipOut.write(_byte, 0, len);
}
_in.close();
zipOut.closeEntry();
}
}
} /**
* 解压缩ZIP文件,将ZIP文件里的内容解压到targetDIR目录下
* @param zipPath 待解压缩的ZIP文件名
* @param descDir 目标目录
*/
public static List<File> upzipFile(String zipPath, String descDir) {
return upzipFile( new File(zipPath) , descDir ) ;
} /**
* 对.zip文件进行解压缩
* @param zipFile 解压缩文件
* @param descDir 压缩的目标地址,如:D:\\测试 或 /mnt/d/测试
* @return
*/
@SuppressWarnings("rawtypes")
public static List<File> upzipFile(File zipFile, String descDir) {
List<File> _list = new ArrayList<File>() ;
try {
ZipFile _zipFile = new ZipFile(zipFile , "GBK") ;
for( Enumeration entries = _zipFile.getEntries() ; entries.hasMoreElements() ; ){
ZipEntry entry = (ZipEntry)entries.nextElement() ;
File _file = new File(descDir + File.separator + entry.getName()) ;
if( entry.isDirectory() ){
_file.mkdirs() ;
}else{
byte[] _byte = new byte[1024] ;
File _parent = _file.getParentFile() ;
if( !_parent.exists() ){
_parent.mkdirs() ;
}
InputStream _in = _zipFile.getInputStream(entry);
OutputStream _out = new FileOutputStream(_file) ;
int len = 0 ;
while( (len = _in.read(_byte)) > 0){
_out.write(_byte, 0, len);
}
_in.close();
_out.flush();
_out.close();
_list.add(_file) ;
}
}
} catch (IOException e) {
}
return _list ;
} /**
* 对临时生成的文件夹和文件夹下的文件进行删除
*/
public static void deletefile(String delpath) {
try {
File file = new File(delpath);
if (!file.isDirectory()) {
file.delete();
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + File.separator + filelist[i]);
if (!delfile.isDirectory()) {
delfile.delete();
} else if (delfile.isDirectory()) {
deletefile(delpath + File.separator + filelist[i]);
}
}
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
upzipFile("steven.zip","D://test");
} }
以下是Java api 中Java.util.zip包下面自带的压缩和解压缩的工具包实现的代码(如果文件名存在非英文编码会出现问题):
package testzip; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import org.apache.commons.io.IOUtils; public class ZipTest { /**
* Unzip it
*
* @param zipFile
* input zip file
* @param output
* zip file output folder
* @throws IOException
*/
public static void unZipIt(String file, String outputFolder)
throws IOException {
ZipFile zipFile = new ZipFile(file);
try {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
File entryDestination = new File(outputFolder, entry.getName());
if (entry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
InputStream in = zipFile.getInputStream(entry);
OutputStream out = new FileOutputStream(entryDestination);
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
out.close();
}
}
} finally {
zipFile.close();
}
} public static void main(String[] args) throws Exception {
unZipIt("test.zip","D://test");
}
}
Java实现压缩文件与解压缩文件的更多相关文章
- 用命令提示符压缩文件,解压缩文件(不需要客户端安装7zip)
压缩成一个CAB包的办法: type list.txt (生成一个文件列表) makecab /f list.txt /d compressiontype=mszip /d compressionme ...
- Java编程的逻辑 (64) - 常见文件类型处理: 属性文件/CSV/EXCEL/HTML/压缩文件
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
- Java实现解压缩文件和文件夹
一 前言 项目开发中,总会遇到解压缩文件的时候.比如,用户下载多个文件时,服务端可以将多个文件压缩成一个文件(例如xx.zip或xx.rar).用户上传资料时,允许上传压缩文件,服务端进行解压读取每一 ...
- 【Linux】【二】linux 压缩文件(txt)、查看压缩文件内容、解压缩文件、
通过Xshell 压缩文件.解压缩文件 gzip tools.txt 压缩[tools.txt]文件 zcat tools.txt.gz 查看压缩文件[tools.txt.gz]内容 gunzip ...
- ZIP解压缩文件的工具类【支持多级文件夹|全】
ZIP解压缩文件的工具类[支持多级文件夹|全] 作者:Vashon 网上有非常多的加压缩演示样例代码.可是都仅仅是支持一级文件夹的操作.假设存在多级文件夹的话就不行了. 本解压缩工具类经过多次检查及重 ...
- ZIP解压缩文件的工具类【支持多级目录|全】
ZIP解压缩文件的工具类[支持多级目录|全] 作者:Vashon 网上有很多的加压缩示例代码,但是都只是支持一级目录的操作,如果存在多级目录的话就不行了.本解压缩工具类经过多次检查及重构,最终分享给大 ...
- C#工具类:使用SharpZipLib进行压缩、解压文件
SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...
- LInux 解压缩文件
常用命令有2个,一个是tar,一个是zip,二选一就行 有的服务器没有安装zip命令,就只有tar可以用,我个人建议还是安装一个zip好一些,tar实在太繁琐 1.解压 tar -zxvf ./xxx ...
- java实现单个或多个文件的压缩、解压缩 支持zip、rar等格式
代码如下: package com.cn.util; import java.io.BufferedInputStream; import java.io.File; import java.io.F ...
随机推荐
- Erlang模块gen_server翻译
gen_server 概要: 通用服务器行为描述: 行为模块实现服务器的客户端-服务器关系.一个通用的服务器进程使用这个模块将实现一组标准的接口功能,包括跟踪和错误报告功能.它也符合OTP进程监控树. ...
- ECMASCRIPT5新特性(转载)
Function 1: Object.create 这是一个很重要的改动,现在我们终于可以得到一个原型链干净的对象了.以前要创建一个类 function Cat(name) { this.name ...
- 查看apache,mysql,nginx,php的编译参数
查看nginx编译参数:/usr/local/nginx/sbin/nginx -V 查看apache编译参数:cat /usr/local/apache2/build/config.nice 查看m ...
- 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
- iOS面试必看经典试题分析
> **不用临时变量怎么实现两个数据的交换?** 方式一:加减法的运算方式求解new_b = a - b + b = a;new_a = a + b - a = b;一个简单的运算方式,最重要的 ...
- [SinGuLaRiTy] 树形存储结构阶段性测试
[SinGuLaRiTy-1011] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. G2019级信息奥赛专项训练 题目 程序名 时间 内存 ...
- VB中的GDI编程-2 画笔
p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...
- 老李案例分享:MAT分析应用程序服务出现内存溢出过程
老李案例分享:MAT分析应用程序服务出现内存溢出过程 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的loa ...
- css浮动布局
上次我们一起对盒子模型进行了一定的了解,今天我们就对css浮动布局做一下研究.首先我们来了解一下网页基本布局的三种形式. 首先我们来了解一下什么是网页布局: 网页的布局方式其实就是指浏览器是如何对网页 ...
- MongoDB基础教程系列--第七篇 MongoDB 聚合管道
在讲解聚合管道(Aggregation Pipeline)之前,我们先介绍一下 MongoDB 的聚合功能,聚合操作主要用于对数据的批量处理,往往将记录按条件分组以后,然后再进行一系列操作,例如,求最 ...