java高级教程实战篇:Java压缩与解压缩详解
先说说压缩的原理:计算机处理信息是以二进制数(0和1)的形式标示的,压缩软件把二进制信息中相同的字符串以特殊字符标记起来压缩的,从而实现缩小文件大小的。好处:可以缩小文件的体积;压缩比则根据文件的不同有所不同,比如文字类的文件压缩比很大(一个字或者一个词在同一个文件会反复出现),而图形图像的压缩比则比较小(每一个像素点的色素不同是常有的,不同颜色和颜色深浅、对比度的不同、亮度不同等的对应二进制代码是不同的)。当然除了可以减小文件体积,方便程序间数据传输压力,还有个好处就是,压缩时可以设置密码,这样即使电脑中病毒,病毒是无法入侵设有密码的压缩文件的。压缩文件的坏处:1、你需要解压工具才能打开文件;2、如果压缩文件有损坏的话,可能是整个文件都受影响。
还好,JDK的util.zip包下提供了这样的功能,今天研究了下,把代码共享如下:

package test;
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.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
/**
* @author Lanxiaowei
* @createTime Jul 21, 2011 19:01:25 PM
*/
public class ZipUtil {
/**
* 压缩
* @param source 源文件(夹)路径
* @param dest 目标文件(夹)路径
*/
public static void zip(String source, String dest) throws IOException {
OutputStream os = new FileOutputStream(dest);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos);
zos.setEncoding("GBK");
File file = new File(source);
String basePath = null;
if (file.isDirectory()) {
basePath = file.getPath();
} else {
basePath = file.getParent();
}
zipFile(file, basePath, zos);
zos.closeEntry();
zos.close();
}
/**
* 解压缩
* @param source 源压缩文件路径
* @param dest 解压路径
*/
public static void unzip(String zipFile, String dest) throws IOException {
ZipFile zip = new ZipFile(zipFile);
Enumeration<ZipEntry> en = zip.getEntries();
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();
}
private static void zipFile(File source, String basePath,
ZipOutputStream zos) throws IOException {
File[] files = new File[0];
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}
String pathName;
byte[] buf = new byte[1024];
int length = 0;
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());
System.out.println(pathName);
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) != -1) {
zos.write(buf, 0, length);
}
is.close();
}
}
}
}
代码中用到了apache提供的ant.jar中的相关类,主要是解决文件中文乱码问题。QQ上传附件还必须开通黄钻,小小鄙视下腾讯,变相收费无处不在。ant.jar包可以自己网上下载,不过可以解压ant.jar只保留zip包下的类,因为那些用不到,然后重新打包即可。这样做的宗旨是尽量不引入无关紧要的类库,当然你不精简也无可厚非哈,我比较追求完美,不喜欢冗余。(*^__^*) 嘻嘻。今天就到此了。
本文来源 我爱IT技术网 http://www.52ij.com/jishu/112.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
