java 中压缩和解压文件的方法
来源:互联网 发布:知乎 经略幽燕我童贯 编辑:程序博客网 时间:2024/04/26 07:26
调用ant.jar里的方法可以完好的实现包括中文在内的zip的压缩与解压
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.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
/**
* @author Every E-mail/MSN:mwgjkf@hotmail.com
* QQ:30130942
* @version 创建时间:Feb 26, 2009 6:01:11 PM
* 类说明:解压文件
*
*/
public class Decompression {
private static final int BUFFEREDSIZE = 1024;
public Decompression() {
// TODO Auto-generated constructor stub
}
/**
* 解压zip格式的压缩文件
*
* @param zipFileName
* 压缩文件
* @param extPlace
* 解压目录
* @throws Exception
*/
public static void unzip(String zipFileName, String extPlace)
throws Exception {
try {
ZipFile zipFile = new ZipFile(zipFileName);
Enumeration e = zipFile.getEntries();
ZipEntry zipEntry = null;
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
String entryName = zipEntry.getName();
String names[] = entryName.split("/");
int length = names.length;
String path = extPlace + File.separator;
for (int v = 0; v < length; v++) {
if (v < length - 1) {
path += names[v] + "/";
System.out.println(path);
new File(path).mkdirs();
} else { // 最后一个
if (entryName.endsWith("/")) { // 为目录,则创建文件夹
new File(extPlace + File.separator + entryName).mkdirs();
} else {
InputStream in = zipFile.getInputStream(zipEntry);
File outentity = new File(extPlace+ File.separator +entryName);
if(outentity.exists()){
outentity.createNewFile();
}
OutputStream os = new FileOutputStream(outentity);
byte[] buf = new byte[BUFFEREDSIZE];
int len;
while ((len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
in.close();
os.close();
}
}
}
}
zipFile.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 压缩zip格式的压缩文件
* @param inputFilename 压缩的文件或文件夹及详细路径
* @param zipFilename 输出文件名称及详细路径
* @throws IOException
*/
public synchronized void zip(String inputFilename, String zipFilename) throws IOException {
zip(new File(inputFilename), zipFilename);
}
/**
* 压缩zip格式的压缩文件
* @param inputFile 需压缩文件
* @param zipFilename 输出文件及详细路径
* @throws IOException
*/
public synchronized void zip(File inputFile, String zipFilename) throws IOException {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));
try {
zip(inputFile, out, "");
} catch (IOException e) {
throw e;
} finally {
out.close();
}
}
/**
* 压缩zip格式的压缩文件
* @param inputFile 需压缩文件
* @param out 输出压缩文件
* @param base 结束标识
* @throws IOException
*/
@SuppressWarnings("unused")
private synchronized void zip(File inputFile, ZipOutputStream out, String base) throws IOException {
if (inputFile.isDirectory()) {
File[] inputFiles = inputFile.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < inputFiles.length; i++) {
zip(inputFiles[i], out, base + inputFiles[i].getName());
}
} else {
if (base.length() > 0) {
out.putNextEntry(new ZipEntry(base));
} else {
out.putNextEntry(new ZipEntry(inputFile.getName()));
}
FileInputStream in = new FileInputStream(inputFile);
try {
int c;
byte[] by = new byte[BUFFEREDSIZE];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
} catch (IOException e) {
throw e;
} finally {
in.close();
}
}
}
}
- java 中压缩和解压文件的方法
- python中压缩和解压文件的方法
- java中对文件解压和压缩的实现
- Java文件的压缩和解压操作
- java实现的文件压缩和解压
- java压缩和解压文件
- 收集总结java中压缩、解压文件方法
- 文件的压缩和解压
- 文件的压缩和解压
- Linux文件压缩和解压方法
- Java解压和压缩带密码的zip文件
- java使用Apache的ant压缩和解压文件(zip)
- java程序实现对文件的压缩和解压
- Java解压和压缩带密码的zip文件
- Java 文件或者文件夹的压缩和解压
- Java实现Zip文件的解压和压缩_ZipUtil
- 文件的压缩和解压(java工具类)
- 7z压缩和解压的方法--java
- Self-Regulation
- How to get parameters from the URL?
- 第一周
- Axure RP Pro - Prototype原型 - 关闭Internet Explorer打开原型时出现的安全警告
- POJ3414 Pots
- java 中压缩和解压文件的方法
- masm开发中@@.@F,@B
- Just get started!
- 怎样取得DBGridEh的footer中的字段合计值
- 企业信息系统的快速建模与开发——课程介绍
- 根据IP地址返回域名
- 中创09校园招聘试题
- extjs 自定义表单验证函数
- 00