Java 的zip压缩和解压缩
package com.meritit.utils.zip; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream; /**
* @function 实现把指定文件夹下的所有文件压缩为指定文件夹下指定 zip 文件&&实现把指定文件夹下的 zip 文件解压到指定目录下
* @createDate:2013-09-18
* @author Ysjian
*/
public final class ZIPUtils { /**
* 测试
*
* @param args
* @throws IOException
* @throws ZipException
*/
public static void main(String[] args) throws ZipException, IOException {
// 把E:\\Ysjian_Job\\osgistart文件夹下的所有文件压缩到 E:\\test.jar中
zip("E:\\test.xlsx", "E:\\test.jar");
// 把E:\\test.jar 压缩文件内的所有文件解压到 E:\\test目录下面
unZip("E:\\test.jar", "E:\\test");
} private ZIPUtils() {
} /**
* sourceFile 文件进行 zip
* 格式的压缩,如果sourceFile是目录,就将sourceFile目录下所有的文件及子目录压缩,如果sourceFile是文件
* ,将sourceFile文件压缩,保存为指定 .zip或者.jar 文件
*
* @param sourceFile
* 源文件,
* @param savedFile
* 保存的文件,可以是.zip或者.jar形式
*
*/
public static void zip(String sourceFile, String savedFile) {
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(savedFile)));
File file = new File(sourceFile);
@SuppressWarnings("unused")
boolean zip = file.isDirectory() ? zipFile(file, file.getPath(),
zos) : zipFile(file, file.getParent(), zos);
} catch (FileNotFoundException e) {
throw new RuntimeException("filePath is invalid-->" + savedFile, e);
} finally {
try {
zos.closeEntry();
zos.close();
} catch (IOException e) {
throw new RuntimeException("fail to close the ZipOutputStream",
e);
}
}
} /**
* 压缩文件,利用到了递归
*
* @param source
* @param basePath
* @param zos
* @throws IOException
*/
private static boolean zipFile(File source, String basePath,
ZipOutputStream zos) {
File[] files = null;
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
} String path = null;
byte[] buf = new byte[1024];
int length = 0;
BufferedInputStream bis = null;
try {
for (File file : files) {
if (file.isDirectory()) {
path = file.getPath().substring(basePath.length() + 1)
+ "/";
zos.putNextEntry(new ZipEntry(path));
zipFile(file, basePath, zos);
} else {
path = file.getPath().substring(basePath.length() + 1);
bis = new BufferedInputStream(new FileInputStream(file));
zos.putNextEntry(new ZipEntry(path));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
throw new RuntimeException(
"fail to close the ZipOutputStream", e);
}
}
}
return true;
} /**
* 解压 zip 文件,只能解压标准的 zip文件
*
* @param sourceFile
* 标准的 zip 文件,不能是把 rar 的直接改为 zip 这样会出现java.io.IOException
* @param targetDir
* 存放的目錄
* @throws ZipException
* @throws IOException
*/
@SuppressWarnings("resource")
public static void unZip(String sourceFile, String targetDir)
throws ZipException, IOException { targetDir = targetDir.endsWith("//") ? targetDir : targetDir + "//";
byte b[] = new byte[1024];
ZipFile zipFile = null;
BufferedOutputStream bos = null;
zipFile = new ZipFile(new File(sourceFile));
Enumeration<?> enumeration = zipFile.entries(); while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
File loadFile = new File(targetDir + zipEntry.getName()); if (!zipEntry.isDirectory()) {
if (!loadFile.getParentFile().exists()) {
loadFile.getParentFile().mkdirs();
}
OutputStream os = new FileOutputStream(loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
bos = new BufferedOutputStream(os); int length = 0;
while ((length = inputStream.read(b)) > 0) {
bos.write(b, 0, length);
}
}
}
}
}
最近工作利用了OSGi,需要在代码中手动生成jar包,也就是bundle,所以采用了这种技术去生成jar包。利用了zip压缩,将事先用eclipse生成的bundle的jar包解压出来,在向里面添加自己的文件,然后在压缩成jar。
Java 的zip压缩和解压缩的更多相关文章
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
reference : http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...
- Java用ZIP格式压缩和解压缩文件
转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...
- java采用zip方式实现String的压缩和解压缩CompressStringUtil类
CompressStringUtil类:不多说,直接贴代码: /** * 压缩 * * @param paramString * @return */ public static final byte ...
- Java对zip格式压缩和解压缩
Java对zip格式压缩和解压缩 通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压. 1.1 ZIP和GZIP的区别 gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格 ...
- Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)
Java ZIP压缩和解压缩文件(解决中文文件名乱码问题) 学习了:http://www.tuicool.com/articles/V7BBvy 引用原文: JDK中自带的ZipOutputStrea ...
- IO操作之使用zip包压缩和解压缩文件
转自:http://www.cdtarena.com/java.htmlJava API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使 ...
- 使用commons-compress操作zip文件(压缩和解压缩)
http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...
- java工具类——java将一串数据按照gzip方式压缩和解压缩
我要整理在工作中用到的工具类分享出来,也方便自己以后查阅使用,这些工具类都是我自己实际工作中使用的 import java.io.ByteArrayInputStream; import java.i ...
- Linux常用命令学习3---(文件的压缩和解压缩命令zip unzip tar、关机和重启命令shutdown reboot……)
1.压缩和解压缩命令 常用压缩格式:.zip..gz..bz2..tar.gz..tar.bz2..rar .zip格式压缩和解压缩命令 zip 压缩文件名 源文件:压缩文件 ...
随机推荐
- Java的IO以及线程练习
文件的IO操作: 字节流: 输入字节流: InputStream 所有输入字节流的基类,抽象类. FileInputStream 读取文件的输入字节流. BufferedInputStream ...
- Hibernaate事务管理
Hibernate使用session时需要继承HibernateDaoSupport对象 HibernateDaoSupport对象中包含默认的getSession()方法,但不可以通过该方法直接启动 ...
- hdu 2822 Dogs(优先队列)
题目链接:hdu2822 会优先队列话这题很容易AC.... #include<stdio.h> #include<string.h> #include<queue> ...
- 网页http请求的整个过程
这几天看一个讲解一个网页从我们输入地址到显示在我们面前的一个讲解,是我对http又有了一个完整的了解,现在做一下整个流程的记录,虽然不是很详细,但是整个过程是完整的.如果不对,请指正! 打开浏览器,地 ...
- System.AccessViolationException: 尝试读取或写入受保护的内存 解决办法
netsh winsock reset --运行此命令解决 错误描述: 之前装的vs2010后 再又安装了vs2013 ,运行之前的vs2010项目 就出现以上错误 错误应用程序名称: w3wp. ...
- 多个Activity和Intent(转)
根据www.mars-droid.com:Andriod开发视频教学,先跳过书本<Beginning Android 2>的几个章,我是这两个资源一起看,需要进行一下同步.先初步了解一下应 ...
- Android布局自定义Shap圆形ImageView,可以单独设置背景与图片
一.图片预览: 一.实现功能: 需求要实现布局中为圆形图片,图片背景与图标分开且合并到一个ImageView. 二.具体实现: XML中布局中定义ImageView, ...
- SQL 用中文的拼音和笔画排序
SQL 用中文的拼音和笔画排序 城市按拼音排序: SELECT chineseName FROM [表名] order by chinesename collate Chinese_PRC_CS_ ...
- iOS-设计模式之通知
通知设计模式简单好用,就是一个项目中如果用的太多,不利于代码维护,可读性太差. 实现过程: [[NSNotificationCenter defaultCenter]postNotificationN ...
- 批量数据上传的sql.xml
<!-- User.xml --><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE ...