`
chenbj920
  • 浏览: 3615 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

ZIP 压缩/解压文件

 
阅读更多
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.apache.tools.zip.ZipEntry;
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.util.Enumeration;

public class ZIPHelper {

private static Logger logger = Logger.getLogger(ZIPHelper.class);

public static final String ENCODING_GBK = "GBK";
public static final String ENCODING_UTF8 = "UTF-8";
public static final String ENCODING_ISO = "ISO-8859-1";

public static void zipped(String filePath)throws IOException {
zipped(filePath, false);
}
public static void zipped(String filePath,boolean isDelSource)throws IOException {
zipped(filePath, null,isDelSource);
}
public static void zipped(String filePath,String regex, boolean isDelSource)throws IOException {
zipped(filePath, regex,null,isDelSource);
}
public static void zipped(String filePath,String regex, String zipPath,boolean isDelSource)throws IOException {
zipped(filePath, regex,zipPath,ENCODING_GBK,isDelSource);
}
public static void zipped(String filePath,String regex, String zipPath,
String encoding,boolean isDelSource)throws IOException {
logger.debug("zipped...." + filePath);
ZipOutputStream zos = null;
File file = new File(filePath);
try {
if(zipPath == null){
if(file.isDirectory()){
zipPath = file.getPath()+".zip";
}else{
zipPath = file.getParent()+"/"+file.getName()+".zip";
}
logger.debug("zipped...." + zipPath);
zos = new ZipOutputStream(new FileOutputStream(zipPath));
}
else{
zos = new ZipOutputStream(new FileOutputStream(zipPath));
}

zos.setEncoding(encoding);
zippedFile(file, zos, "",regex,isDelSource);

} catch (FileNotFoundException e) {
logger.error("zipped ..."+e.getMessage());
throw e;
} finally {
try {
if (zos != null){
zos.close();
}
} catch (IOException e) {
logger.error("zipped ..."+e.getMessage());
throw e;
}
}
logger.debug("zipped ok....");
}

private static void zippedFile(File f, ZipOutputStream zos,
String hiberarchy,String regex,boolean isDelSource) throws IOException {
logger.debug("zipped...."+f.getName());
if (f.exists()) {
if (f.isDirectory()) {
hiberarchy += f.getName() + "/";
File[] fif = f.listFiles();
for (int i = 0; i < fif.length; i++) {
if(regex != null){
if(fif[i].getName().matches(regex)){
zippedFile(fif[i], zos, hiberarchy,regex,isDelSource);
}
}else{
zippedFile(fif[i], zos, hiberarchy,regex,isDelSource);
}
if(isDelSource){
if(!fif[i].delete()){
logger.debug("Delete files defeat..."+fif[i].getName());
}
}
}
} else {

FileInputStream fis = null;
try {
fis = new FileInputStream(f);
ZipEntry ze = new ZipEntry(hiberarchy + f.getName());
zos.putNextEntry(ze);
byte[] b = new byte[1024];
int slen;
while ((slen = fis.read(b, 0, b.length)) != -1)
zos.write(b, 0, slen);
} finally {
try {
if (fis != null)
fis.close();    
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
}
}





public static void unzip(String zipPath) throws IOException {
unzip(zipPath, false);
}
public static void unzip(String zipPath, boolean isDelZip)throws IOException {
unzip(zipPath, null,isDelZip);
}
public static void unzip(String zipPath, String extractPath, boolean isDelZip) throws IOException {
unzip(zipPath, extractPath, ENCODING_GBK,isDelZip);
}
public static void unzip(String zipPath, String extractPath,String encoding, boolean isDelZip) throws IOException {

logger.debug("unzip...." + zipPath);
File infile = new File(zipPath);
ZipFile zip = new ZipFile(infile,encoding);
Enumeration<ZipEntry> enums = zip.getEntries();

File destFile = null;
if(extractPath == null){
extractPath = infile.getParent();
}
while(enums.hasMoreElements()){
ZipEntry entry = enums.nextElement();
logger.debug("extract...."+entry.getName());
destFile = new File(extractPath, entry.getName());
unzipFile(destFile,zip,entry);
}
zip.close();
if(isDelZip){
if(!infile.delete()){
logger.debug("Delete files defeat..."+infile.getName());
}
}
logger.debug("unzip ok....");
}

private static void unzipFile(File destFile,ZipFile zip,ZipEntry entry) throws IOException {
try{
if(entry.isDirectory()){
destFile.mkdirs();
}else{
File file = destFile.getParentFile();
if(file != null && !file.exists()){
file.mkdirs();
}
FileOutputStream output = new FileOutputStream(destFile);
byte[] c = new byte[1024];
int slen;
InputStream input = zip.getInputStream(entry);
while ((slen = input.read(c, 0, c.length)) != -1)
output.write(c, 0, slen);

input.close();
output.close();
}
} catch (FileNotFoundException e) {
logger.error("unzip ..."+e.getMessage());
throw e;
}
}
}
分享到:
评论

相关推荐

    PHP在线压缩/解压文件 PHPZIP 免费版.rar

    phpZip 是一个压缩/解压工具,是专为加快与服务器之间上传或下载文件而设计的。其最大的特点是附带PHP的解压/压缩程序,也就是说,本地压缩的程序可以在服务器解压缩,也可以将服务器端的文件打包下载,然后在本地...

    Java zip 压缩/解压源码

    这个`Java zip 压缩/解压源码`的资源提供了一个简洁易用的API,使得开发者能够方便地对文件进行压缩和解压缩操作。下面我们将深入探讨Java中处理`zip`文件的相关知识。 1. **Java ZipFile类**: `java.util.zip....

    压缩/解压文件(支持rar,zip)

    本文将详细讨论如何使用CS类来实现RAR和ZIP格式的压缩与解压,以及在ASP.NET环境中进行在线操作的相关技术。 首先,RAR和ZIP是两种广泛使用的文件压缩格式。RAR是由RARLAB开发的,它提供了更高的压缩比,可以修复...

    使用 Java 实现的压缩/解压 ZIP 文件的工具类

    本文将深入探讨如何使用Java实现ZIP文件的压缩与解压,重点讲解核心API,如`java.util.zip`包中的`ZipOutputStream`和`ZipInputStream`。 首先,我们来了解一下ZIP文件格式。ZIP是一种用于存储多个文件到一个单一...

    zip压缩/解压缩文件

    zip压缩/解压缩文件

    C#压缩/解压ZIP文件带密码功能

    在C#编程环境中,处理ZIP文件的压缩与解压是一项常见的任务,特别是在文件管理和数据传输中。本篇文章将深入探讨如何使用C#实现ZIP文件的压缩和解压,并且涵盖如何为ZIP文件设置密码,以增强文件的安全性。 首先,...

    Delphi实现Zip压缩/解压缩

    总之,Delphi的`System.Zip`单元为开发者提供了强大的ZIP文件操作能力,无论是创建新的ZIP文件、添加文件还是解压缩文件都变得轻而易举。同时,通过COM接口,我们可以让不同版本的Delphi之间共享这些功能,大大提升...

    java 压缩/解压 .zip/.rar/.tar 文件

    本文将详细讲解如何使用Java API来压缩和解压缩`.zip`、`.rar`和`.tar`这三种常见的文件格式。 首先,对于`.zip`文件的处理,我们可以使用Apache的`commons-compress`库中的`ZipFile`和`ZipOutputStream`类。在提供...

    ZIP格式压缩/解压系统设计(JAVA)

    通过设计,允许创建ZIP压缩文件,并对ZIP压缩文件中包含的文件进行显示、添加、解压、删除等操作。GUI界面与下图类似: 【实验目的】 要求学生能熟练使用基于Swing的GUI设计,熟练使用常用组件和容器,理解java事件...

    压缩/解压程序

    例如,对于ZIP文件,程序可能包含了解析ZIP头信息、读取每个压缩块并使用DEFLATE算法解压的逻辑。 在“工具类”这个标签下,我们可以推断这个程序设计为通用且易于使用的工具。通常,此类程序会包含用户友好的界面...

    使用Windows自带程序压缩/解压文件

    ### 使用Windows自带程序压缩/解压文件 #### 知识点概述 本文主要介绍如何利用Windows自带的功能进行压缩与解压操作,特别是针对Zip格式的文件。通过VBA宏编程,用户可以在Excel环境中轻松实现对Zip文件的解压缩。...

    在线PHP压缩/解压程序(zip)

    在提供的文件列表中,我们看到只有一个名为"zip.php"的文件,这很可能是程序的核心部分,负责执行压缩和解压的逻辑。 PHP是一种广泛使用的服务器端脚本语言,尤其适用于Web开发。在PHP中处理文件压缩和解压通常涉及...

    基于quazip源码的压缩/解压Qt案例,实现了压缩及解压进度显示

    quazip下载不了,或者zip.lib无法下载,这里可以完美解决你的问题,此工程包含quazip、zip.c与unzip.c源码,此源码可以编译成lib库,也可以直接使用,基于quazip,新增了压缩与解压进度显示,Qt5.6.3+MSVC2013的demo...

    Zip压缩和解压文件

    本文将深入探讨Zip压缩和解压的相关知识点,并提供一个参考示例。 首先,我们要理解Zip压缩的工作原理。Zip采用了多种压缩算法,如DEFLATE,这是一种基于LZ77的无损数据压缩算法,通过查找和替换重复的数据模式来...

    Zip2Dir V2.01 绿色中文版_压缩/解压/测试/注释ZIP.rar

    电子版漫画整理工具,能够批量压缩/解压/测试/注释zip文件,支持右健菜单,具有密码记忆功能。 动机: 在整理收藏的电子版漫画的时候,希望能够有这样的软件: 1、能够自动将指定目录下的所有zip、exe文件全部...

    ZIP压缩和解压类

    - 遍历文件/目录:对源目录进行递归遍历,获取所有文件和子目录的路径。 - 打开ZIP流:创建一个可写入的ZIP文件流,准备写入压缩数据。 - 压缩文件:对每个文件调用压缩算法,将结果写入ZIP流。 - 关闭ZIP流:...

    c#处理压缩/解压类文件

    c#处理压缩/解压类文件

    SevenZipSharp压缩/解压(.7z .zip)

    【描述】:描述中提到的“SevenZipSharp加密压缩/解压示范性Demo”,说明这个项目提供了一个示例,展示如何使用SevenZipSharp进行加密压缩和解压文件。加密功能增强了数据安全性,确保敏感信息不被未授权的用户访问...

    测试 Qt 使用Quazip 压缩/解压ZIP文件 的可执行程序案例,不是源码

    在本文中,我们将深入探讨如何在Qt环境中使用Quazip库进行ZIP文件的压缩与解压操作。Quazip是一个开源的C++库,它为Qt应用程序提供了方便的API,以便处理ZIP和GZIP格式的文件。这个"测试 Qt 使用Quazip 压缩/解压ZIP...

    zip文件压缩解压源码 (c++)

    总结来说,`zip文件压缩解压源码(C++)`涉及的关键技术包括:理解zip文件格式,使用C++进行文件操作,选择和应用压缩算法,以及错误处理。通过分析和理解`XUnzip.cpp`和`XZip.cpp`,我们可以学习如何在C++环境中...

Global site tag (gtag.js) - Google Analytics