java写的zip压缩程序
/**
* @author HJX
* @version 1.0,2014-01-14
* @since JDK1.7
* java写的zip压缩程序
* ZipCompress
* ZipCompress-main
* ZipCompress-compressFileHelper
* ZipCompress-compressFile
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//apache的zip可以防止出现中文乱码的情况
//java的zip则会出现中文乱码
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class ZipCompress {
/**
* @param inputFileName [outputFileName]
* @return void
*/
public static void main(String[] args) {
// 判断参数合法性
if ((args.length == 0) || (args.length > 2)) {
System.err.println("Usage:CompressTest inputFileName [outputFileName]");
System.exit(-1);
}
// 开始压缩的时刻
double startTime = System.currentTimeMillis();
ZipCompress compressTest = new ZipCompress();
// 被压缩目标的路径,可以指向一个文件或者一个目录
String srcFile = args[0];
// 生成的压缩文件
String destFile = "";
if (args.length == 2) {
destFile = args[1];
} else {
destFile = srcFile + ".zip";
}
// 相对地址,基地址,根目录
String basePath = "";
ZipOutputStream zos = null;
try {
// 创建一个zip输出流
zos = new ZipOutputStream(new FileOutputStream(destFile));
// 启动压缩进程
compressTest.compressFileHelper(zos, basePath, srcFile);
// 压缩成功的时刻
double endTime = System.currentTimeMillis();
System.out.println("压缩成功!耗时" + (endTime - startTime) / 1000.0 + "s");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (zos != null)
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void compressFileHelper(ZipOutputStream zos, String basePath,
String srcFile) {
File file = new File(srcFile);
if (file.isDirectory()) {
// 如果是目录
try {
zos.putNextEntry(new ZipEntry(basePath + file.getName() + "/"));
zos.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
compressFileHelper(zos, basePath + file.getName() + "/",
files[i].getPath());
} else {
compressFile(zos, basePath, file);
}
}
public void compressFile(ZipOutputStream zos, String basePath, File srcFile) {
FileInputStream fis = null;
try {
fis = new FileInputStream(srcFile);
zos.putNextEntry(new ZipEntry(basePath + srcFile.getName()));
int length = 0;
int bufferSize = 1024;
byte[] butter = new byte[bufferSize];
while ((length = fis.read(butter, 0, bufferSize)) >= 0) {
zos.write(butter, 0, length);
}
zos.closeEntry();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
java写的zip解压缩程序
/**
* @author HJX
* @version 1.0,2014-01-14
* @since JDK1.7
* java写的zip解压缩程序
* ZipDecompress
* ZipDecompress-main
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ZipDecompress {
/**
* @param inputFileName [outputFileName]
* @return null
*/
public static void main(String[] args) {
// 判断参数合法性
if ((args.length == 0) || (args.length > 2)) {
System.err.println("Usage:CompressTest inputFileName [outputFileName]");
System.exit(-1);
}
// 开始压缩的时刻
double startTime = System.currentTimeMillis();
ZipDecompress decompressTest = new ZipDecompress();
// 被解压缩的压缩文件的路径
String srcFile = args[0];
// 输出路径
String destFile = "";
if (args.length == 2) {
destFile = args[1];
} else {
destFile = srcFile.substring(0, srcFile.length() - 4);
}
ZipInputStream zis = null;
try {
// 创建一个zip输入流
zis = new ZipInputStream(new FileInputStream(args[0]));
// 输出文件
File file = null;
ZipEntry entry;
int length = 0;
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
FileOutputStream fos = null;
try {
while ((entry = zis.getNextEntry()) != null) {
file = new File(destFile, entry.getName());
if (!entry.isDirectory()) {
if (!file.exists()) {
(new File(file.getParent())).mkdirs();
}
fos = new FileOutputStream(file);
while ((length = zis.read(buffer, 0, bufferSize)) >= 0) {
fos.write(buffer, 0, length);
}
} else {
file.mkdirs();
}
}
zis.closeEntry();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
double endTime = System.currentTimeMillis();
System.out.println("解压成功!耗时:" + (endTime - startTime) / 1000.0 + "s");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (zis != null)
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}