一个压缩及解压缩的类
来源:互联网 发布:焊接机器人编程入门 编辑:程序博客网 时间:2024/04/28 04:42
import java.util.zip.*;
import java.io.*;
class TestZip
{
public void zip(String zipFileName, String inputFile) throws Exception
{
zip(zipFileName, new File(inputFile));
}
public void zip(String zipFileName, File inputFile) throws Exception
{
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
zip(out, inputFile, "");
System.out.println("zip done");
out.close();
}
public void unzip(String zipFileName, String outputDirectory)
throws Exception
{
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry z;
while ((z = in.getNextEntry()) != null)
{
System.out.println("unziping " + z.getName());
if (z.isDirectory())
{
String name = z.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println("mkdir " + outputDirectory + File.separator
+ name);
}
else
{
File f = new File(outputDirectory + File.separator
+ z.getName());
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int b;
while ((b = in.read()) != -1)
out.write(b);
out.close();
}
}
in.close();
}
public void zip(ZipOutputStream out, File f, String base) throws Exception
{
System.out.println("Zipping " + f.getName());
if (f.isDirectory())
{
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++)
{
zip(out, fl[i], base + fl[i].getName());
}
}
else
{
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ((b = in.read()) != -1)
out.write(b);
in.close();
}
}
public static void main(String[] args)
{
try
{
TestZip t = new TestZip();
t.zip("c://test.zip", "c://test");
t.unzip("c://test.zip", "c://test2");
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}
}
- 一个压缩及解压缩的类
- 一个失败的哈夫曼压缩解压缩代码
- 压缩及解压缩命令tar的使用
- 压缩及解压缩
- 如何把一个文件压缩成几个固定大小的文件及解压缩
- 基于Gzip的压缩解压缩帮助类
- 一个压缩解压缩文件的dll的声明说明
- 压缩解压缩的插件
- 项目:文件压缩及解压缩
- linux 压缩及解压缩 命令
- 压缩、解压缩及打包归档
- 用C实现的一个huffman压缩解压缩程序
- libjpeg实现内存内位图的压缩及解压缩
- libpng实现内存内位图的压缩及解压缩
- linux的压缩/解压缩命令(tar,gzip等)及示例
- libjpeg实现内存内位图的压缩及解压缩
- libjpeg实现内存内位图的压缩及解压缩
- libjpeg实现内存内位图的压缩及解压缩
- 工作终于要搞好
- 把javascript,vbscript中得数组传递给COM组件(or Activex)
- 上传文件
- P2P 之 UDP穿透NAT的原理与实现(附源代码)
- 看了一下午的遗传
- 一个压缩及解压缩的类
- 最好的朋友
- 关于建立《文件格式、协议大全》共享版 的想法
- 激活成功啦
- 学习高手源码的乐趣!
- 关于RowSet的使用(1)
- ASP.NET应用程序的安全方案(三)—安全通信
- 文件传输协议(File Transfer Protocol, FTP)
- 一个用到Boost中time_duration类而产生的bug