您的位置:首页 > 编程语言 > Java开发

Java解压缩文件简单实例

2015-08-26 00:00 399 查看
摘要: Java解压缩文件简单实例

package com.cwqi.demo;
/**
*@author Cwqi
*2015-8-26上午9:59:12
*/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

public class ZipDemo {

/**
* 解压
* @param zipFilePath:压缩文件路径
*/
public static void unzip(String zipFilePath) {
unzipFile(zipFilePath, null);
}

public static File buildFile(String fileName, boolean isDirectory) {
File target = new File(fileName);
if (isDirectory) {
target.mkdirs();
} else {
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
}
}
return target;
}

/**
* 解压
* @param zipFilePath zip文件路径
* @param targetPath  解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
*/
public static void unzipFile(String zipFilePath, String targetPath) {
OutputStream os = null;
InputStream is = null;
ZipFile zipFile = null;
try {
zipFile = new ZipFile(zipFilePath);
String directoryPath = "";
if (null == targetPath || "".equals(targetPath)) {
directoryPath = zipFilePath.substring(0,
zipFilePath.lastIndexOf("."));
} else {
directoryPath = targetPath;
}
@SuppressWarnings("unchecked")
Enumeration<ZipEntry> entryEnum = zipFile.getEntries();

if (null != entryEnum) {
ZipEntry zipEntry = null;
while (entryEnum.hasMoreElements()) {
zipEntry = entryEnum.nextElement();
if (zipEntry.isDirectory()) {
new File(directoryPath + File.separator
+ zipEntry.getName()).mkdirs();
continue;
}
if (zipEntry.getSize() > 0) {
// 文件
File targetFile = ZipDemo.buildFile(directoryPath
+ File.separator + zipEntry.getName(), false);
os = new BufferedOutputStream(new FileOutputStream(
targetFile));
is = zipFile.getInputStream(zipEntry);
byte[] buffer = new byte[4096];
int readLen = 0;
while ((readLen = is.read(buffer, 0, 4096)) >= 0) {
os.write(buffer, 0, readLen);
}
os.flush();
os.close();
} else {
// 空目录
ZipDemo.buildFile(directoryPath + File.separator
+ zipEntry.getName(), true);
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (null != zipFile) {
zipFile = null;
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

static final int BUFFER = 2048;

/**
* 压缩
* @param sourceFile 为要压缩的文件或文件目录
* @param targetFile 为要生成zip文件的路径和文件名,如:"D:\\downloads.zip"
*/
public static void zipFiles(String sourceDir, String targetFile) {
File sourceFile = new File(sourceDir);
File zFile = new File(targetFile);
ZipOutputStream zos = null;
try {

OutputStream os;
os = new FileOutputStream(zFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
zos = new ZipOutputStream(bos);

String basePath = null;

if (sourceFile.isDirectory()) {
basePath = sourceFile.getPath();
} else {
basePath = sourceFile.getParent();
}
zipFile(sourceFile, basePath, zos);

} catch (IOException e) {
e.printStackTrace();
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

private static void zipFile(File sourceFile, String basePath,
ZipOutputStream zos) throws IOException {
File[] files = null;
if (sourceFile.isDirectory()) {
files = sourceFile.listFiles();
} else {
files = new File[] { sourceFile };
}

InputStream is = null;
BufferedInputStream bis = null;
String pathName;
byte[] buf = new byte[BUFFER];
int length = 0;
try {
for (File file : files) {
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1)
+ "/";
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
is = new FileInputStream(file);
bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) != -1) {
zos.write(buf, 0, length);
}
}
}
} finally {
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
}

}

/**
* 使用ant压缩
* @param srcPathName
* @param destPathName
*/
public static void zipByAnt(String srcPathName,String destPathName) {
File srcdir = new File(srcPathName);
if (!srcdir.exists()){
throw new RuntimeException(srcPathName + "no exist!");
}
Project project = new Project();
Zip zip = new Zip();
zip.setProject(project);

File zipFile = new File(destPathName);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(project);
fileSet.setDir(srcdir);
// fileSet.setIncludes("**/*.java");
// fileSet.setExcludes(*.txt);
zip.addFileset(fileSet);
zip.execute();
}

/**
* 测试
* @param args
*/
public static void main(String[] args) {
unzipFile("D:\\Downloads.zip", "D:\\Downloads");
//zipFiles("D:\\Downloads", "D:\\Downloads.zip");
//zipByAnt("D:\\Downloads", "D:\\Downloads.zip");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java 解压 压缩