Java解压缩Zip 文件
package com.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class TestZip {
/**
* zip压缩功能测试. 将d:\\temp\\zipout目录下的所有文件连同子目录压缩到d:\\temp\\out.zip.
*
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void CreateZip(String zipUrl, String zipName) throws Exception {
// 压缩baseDir下所有文件,包括子目录
String baseDir = zipUrl;
List fileList = getSubFiles(new File(baseDir));
// 压缩文件名
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipName));
ZipEntry ze = null;
byte[] buf = new byte[1024];
int readLen = 0;
for (int i = 0; i < fileList.size(); i++) {
File f = (File) fileList.get(i);
System.out.print("Adding: " + f.getPath() + f.getName());
// 创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(getAbsFileName(baseDir, f));
ze.setSize(f.length());
ze.setTime(f.lastModified());
// 将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze);
InputStream is = new BufferedInputStream(new FileInputStream(f));
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen);
}
is.close();
System.out.println(" done");
}
zos.close();
}
/**
* 取得指定目录下的所有文件列表,包括子目录.
*
* @param baseDir
* File 指定的目录
* @return 包含java.io.File的List
*/
@SuppressWarnings("unchecked")
private List getSubFiles(File baseDir) {
List ret = new ArrayList();
// File base=new File(baseDir);
File[] tmp = baseDir.listFiles();
for (int i = 0; i < tmp.length; i++) {
if (tmp[i].isFile()) {
ret.add(tmp[i]);
}
if (tmp[i].isDirectory()) {
ret.addAll(getSubFiles(tmp[i]));
}
}
return ret;
}
/**
* 给定根目录,返回另一个文件名的相对路径,用于zip文件中的路径.
*
* @param baseDir
* java.lang.String 根目录
* @param realFileName
* java.io.File 实际的文件名
* @return 相对文件名
*/
private String getAbsFileName(String baseDir, File realFileName) {
File real = realFileName;
File base = new File(baseDir);
String ret = real.getName();
while (true) {
real = real.getParentFile();
if (real == null)
break;
if (real.equals(base))
break;
else {
ret = real.getName() + "/" + ret;
}
}
return ret;
}
public static void main(String[] args) {
TestZip.zipFile();
// TestZip.unzip(url, dest);
}
/**
* 解压缩zip文件
*/
@SuppressWarnings("unchecked")
public static void unzip(String zipFile, String dest) throws Exception {
// String ctxUrl = ServletActionContext.getRequest().getRealPath("/upload");
// String url = ctxUrl + "\\" + fileName;
// String dest = ctxUrl;
// zipFile = url;
ZipFile zip = new ZipFile(zipFile);
Enumeration<ZipEntry> en = (Enumeration)zip.entries();
ZipEntry entry = null;
byte[] buffer = new byte[1024];
int length = -1;
InputStream input = null;
BufferedOutputStream bos = null;
File file = null;
while (en.hasMoreElements()){
entry = (ZipEntry) en.nextElement();
if (entry.isDirectory()) {
file = new File(dest, entry.getName());
if (!file.exists()){
file.mkdir();
}
continue;
}
input = zip.getInputStream(entry);
file = new File(dest, entry.getName());
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
bos = new BufferedOutputStream(new FileOutputStream(file));
while (true){
length = input.read(buffer);
if (length == -1)
break;
bos.write(buffer, 0, length);
}
bos.close();
input.close();
}
zip.close();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class TestZip {
/**
* zip压缩功能测试. 将d:\\temp\\zipout目录下的所有文件连同子目录压缩到d:\\temp\\out.zip.
*
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void CreateZip(String zipUrl, String zipName) throws Exception {
// 压缩baseDir下所有文件,包括子目录
String baseDir = zipUrl;
List fileList = getSubFiles(new File(baseDir));
// 压缩文件名
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipName));
ZipEntry ze = null;
byte[] buf = new byte[1024];
int readLen = 0;
for (int i = 0; i < fileList.size(); i++) {
File f = (File) fileList.get(i);
System.out.print("Adding: " + f.getPath() + f.getName());
// 创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(getAbsFileName(baseDir, f));
ze.setSize(f.length());
ze.setTime(f.lastModified());
// 将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze);
InputStream is = new BufferedInputStream(new FileInputStream(f));
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen);
}
is.close();
System.out.println(" done");
}
zos.close();
}
/**
* 取得指定目录下的所有文件列表,包括子目录.
*
* @param baseDir
* File 指定的目录
* @return 包含java.io.File的List
*/
@SuppressWarnings("unchecked")
private List getSubFiles(File baseDir) {
List ret = new ArrayList();
// File base=new File(baseDir);
File[] tmp = baseDir.listFiles();
for (int i = 0; i < tmp.length; i++) {
if (tmp[i].isFile()) {
ret.add(tmp[i]);
}
if (tmp[i].isDirectory()) {
ret.addAll(getSubFiles(tmp[i]));
}
}
return ret;
}
/**
* 给定根目录,返回另一个文件名的相对路径,用于zip文件中的路径.
*
* @param baseDir
* java.lang.String 根目录
* @param realFileName
* java.io.File 实际的文件名
* @return 相对文件名
*/
private String getAbsFileName(String baseDir, File realFileName) {
File real = realFileName;
File base = new File(baseDir);
String ret = real.getName();
while (true) {
real = real.getParentFile();
if (real == null)
break;
if (real.equals(base))
break;
else {
ret = real.getName() + "/" + ret;
}
}
return ret;
}
public static void main(String[] args) {
TestZip.zipFile();
// TestZip.unzip(url, dest);
}
/**
* 解压缩zip文件
*/
@SuppressWarnings("unchecked")
public static void unzip(String zipFile, String dest) throws Exception {
// String ctxUrl = ServletActionContext.getRequest().getRealPath("/upload");
// String url = ctxUrl + "\\" + fileName;
// String dest = ctxUrl;
// zipFile = url;
ZipFile zip = new ZipFile(zipFile);
Enumeration<ZipEntry> en = (Enumeration)zip.entries();
ZipEntry entry = null;
byte[] buffer = new byte[1024];
int length = -1;
InputStream input = null;
BufferedOutputStream bos = null;
File file = null;
while (en.hasMoreElements()){
entry = (ZipEntry) en.nextElement();
if (entry.isDirectory()) {
file = new File(dest, entry.getName());
if (!file.exists()){
file.mkdir();
}
continue;
}
input = zip.getInputStream(entry);
file = new File(dest, entry.getName());
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
bos = new BufferedOutputStream(new FileOutputStream(file));
while (true){
length = input.read(buffer);
if (length == -1)
break;
bos.write(buffer, 0, length);
}
bos.close();
input.close();
}
zip.close();
}
}