网上的东西整理了下
压缩
方法一
package com.lybj.method;
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.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* java压缩文件的方法一: jdk自带的包java.util.zip.ZipOutputStream,不足之处,文件(夹)名称带中文时,出现乱码问题
*
* @author gwq
*/
public class ZipMethodOne {
/**
*
* @param sourceDir
* 源文件路劲
* @param zipFileDir
* 目标文件路劲
*/
public void doZip(String sourceDir, String zipFileDir) {
ZipOutputStream zos = null;
try {
File file = new File(sourceDir);
File zipFile = new File(zipFileDir);
// 创建写出流
OutputStream os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
zos = new ZipOutputStream(bos);
// 获取目录
String basePath = "";
if (file.isDirectory()) {
basePath = file.getPath();
} else {
basePath = file.getParent();
}
zipFile(file, basePath, zos);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zos != null) {
try {
zos.closeEntry();
zos.close();
zos = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* @param file
* 源文件
* @param basePath
* 基础路劲
* @param zos
* 写出流
* @throws IOException
*/
private void zipFile(File file, String basePath, ZipOutputStream zos)
throws IOException {
File[] files = null;
if (file.isDirectory()) {
files = file.listFiles();
} else {
files = new File[1];
files[0] = file;
}
InputStream is = null;
String pathName = "";
byte[] buf = new byte[1024];
int length = 0;
try {
// 遍历
for (File f : files) {
if (f.isDirectory()) {
pathName = f.getPath().substring(basePath.length() + 1)
+ "/";
zos.putNextEntry(new ZipEntry(pathName));
zipFile(f, basePath, zos);
} else {
pathName = f.getPath().substring(basePath.length() + 1);
is = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
}
}
} finally {
if (is != null) {
is.close();
is = null;
}
}
}
}
方法二:
package com.lybj.method;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* 使用org.apache.tools.zip.ZipOutputStream
* @author Administrator
*
*/
public class ZipMethodTwo {
private final static int BUFFER = 8 * 1024;
/**
* @param sourceDir
* 源文件路径
* @param zipFileDir
* 目标文件路径
*/
public void doZip(String sourceDir, String zipFileDir) {
File sourceFile = new File(sourceDir); // 源文件
File zipFile = new File(zipFileDir); // 目标文件
/* 判断源文件是否存在 */
if (!sourceFile.exists()) {
throw new RuntimeException(sourceDir + "不存在!");
}
ZipOutputStream zos = null;
try {
/* 输出流创建 */
OutputStream os = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(os, new CRC32());
zos = new ZipOutputStream(cos);
zos.setEncoding("UTF-8");
String baseDir = "";
/* 压缩 */
zipFile(sourceFile, baseDir, zos);
} catch (Exception e) {
e.printStackTrace();
} finally {
/* 关闭 */
if (zos != null) {
try {
zos.closeEntry();
zos.close();
zos = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* @param sourceFile
* 源文件
* @param baseDir
* 相对路径
* @param zos
* 输出流
* @throws IOException
*/
private void zipFile(File sourceFile, String baseDir, ZipOutputStream zos)
throws IOException {
/* 判断是否是文件夹 */
if (sourceFile.isDirectory()) {
// 是文件夹的case
File[] files = sourceFile.listFiles();
if (files.length == 0) {
zos.putNextEntry(new ZipEntry(baseDir + sourceFile.getName()
+ "/")); // 空文件夹
} else {
// 循环递归调用
for (File file : files) {
zipFile(file, baseDir + sourceFile.getName() + "/", zos);
}
}
} else {
// 文件的case
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(sourceFile));// 写入流
zos.putNextEntry(new ZipEntry(baseDir + sourceFile.getName()));
byte[] date = new byte[BUFFER];
int length = 0;
while ((length = bis.read(date)) > 0) {
zos.write(date, 0, length);
}
} finally {
if (bis != null) {
bis.close();
bis = null;
}
}
}
}
}
方法三:
package com.lybj.method;
import java.io.File;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
/**
* 可以用ant中的org.apache.tools.ant.taskdefs.Zip来实现
*
* @author gwq
*
*/
public class ZipMethodThree {
/**
* @param sourceDir
* 源文件路径
* @param zipFileDir
* 目标文件路径
*/
public void doZip(String sourceDir, String zipFileDir) {
File sourceFile = new File(sourceDir); // 源文件
File zipFile = new File(zipFileDir); // 目标文件
/* 判断源文件是否存在 */
if (!sourceFile.exists()) {
throw new RuntimeException(sourceDir + "不存在!");
}
Project project = new Project();
Zip zip = new Zip();
zip.setProject(project);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(project);
fileSet.setDir(sourceFile);
// fileSet.setIncludes("*.java");压缩后缀名是java的文件
// fileSet.setExcludes("*.java");排除后缀名是java的文件
zip.addFileset(fileSet);
zip.execute();
}
}
这里方法 二,三都有个问题,当文件目录下有空文件夹目录时,压缩后不能正常显示成文件夹,没找到原因和解决方法。
方法三不能手动创建压缩文件,在ant包封装了,加了会出错的。
解压:
package com.lybj.unzip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
/**
* 利用jdk解压,会出现中文乱码
*
* @author gwq
*
*/
public class UnZipMethod1 {
/**
*
* @param zipFilePath
* 被解压文件路劲
* @param unZipDirectory
* 解压路劲
*/
public void doUnZip(String zipFilePath, String unZipDirectory) {
/* 判断被解压文件是否存在 */
File zipFile = new File(zipFilePath);
if (!zipFile.isFile()) {
throw new RuntimeException(zipFilePath + "不存在");
}
/* 判断解压路劲是否存在,不存在就创建 */
String dirName = zipFile.getName().substring(0,
zipFile.getName().indexOf(".zip"));
File baseDirectory = new File(unZipDirectory + "/" + dirName);
if (!baseDirectory.isDirectory()) {
baseDirectory.mkdir();
}
// 写入写出流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
ZipFile zip = new ZipFile(zipFile);
// 循环
Enumeration zipEnu = zip.entries();
while (zipEnu.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) zipEnu.nextElement();
String entryName = zipEntry.getName();
if (zipEntry.isDirectory()) {
// 是文件夹就加一层目录
new File(baseDirectory + "/" + entryName).mkdir();
} else {
bis = new BufferedInputStream(zip.getInputStream(zipEntry));// 得到写入流
bos = new BufferedOutputStream(new FileOutputStream(
baseDirectory.getAbsolutePath() + "/" + entryName)); // 创建写出流
byte[] data = new byte[8 * 1024]; // 缓冲字节
int length = 0;
while ((length = bis.read(data)) > 0) {
bos.write(data, 0, length);
}
}
}
} catch (ZipException e) {
e.printStackTrace();// 最好用打log替换,
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
bos = null;
}
if (bis != null) {
bis.close();
bis = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这是jdk自带的,网上说不能解决中文乱码问题,我自己跑了下,没出现乱码,不知道是不是由于环境问题。
还有就是用apache的ant包,代码都差不多,就是把包换一下。
附上ant包下载链接:
http://ant.apache.org/bindownload.cgi