Java 实现zip加压解压


package com.common;

import java.io.*;
import java.util.*;
import java.util.zip.*;

public class ZipFiles_20091120
{
static final int BUFFER = 2048; //压缩缓冲区大小
public int files = 0;
public String filename = "";

/**
* 对选定的当前目录下所有文件进行压缩
* @param inputUrl 被压缩文件所在目录路径,路径要已"/"结束
* @param outputUrl 压缩文件输出路径,包括zip文件名称
*/
public void zip(String inputUrl, String outputUrl)
{
try
{
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(outputUrl);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
byte data[] = new byte[BUFFER];
// 得到当前目录下文件列表
File f = new File(inputUrl);
File files[] = f.listFiles();
//compressFiles(inputUrl, files, out, data);

///

for (int i=0; i < files.length; i++)
{

if (files[i].isFile()&&!files[i].getName().equals("temp.zip"))
{
System.out.println("NAME: "+files[i].getName());
System.out.println("Adding: "+files[i]);
FileInputStream fi = new FileInputStream(files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i].getName());
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0, BUFFER)) != -1)
{
out.write(data, 0, count);
}
origin.close();
}
}

///

out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* 对选定的压缩文件进行解压缩。
* @param inputUrl zip压缩文件路径,包括zip文件名称
* @param outputUrl 解压缩文件输出目录路径
*/
public void unZip(String inputUrl, String outputUrl)
{
try
{
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile(inputUrl);
Enumeration e = zipfile.entries();
while (e.hasMoreElements())
{
entry = (ZipEntry) e.nextElement();
is = new BufferedInputStream
(zipfile.getInputStream(entry));
int count;
byte data[] = new byte[BUFFER];
FileOutputStream fos = new
FileOutputStream(outputUrl + entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
while ( (count = is.read(data, 0, BUFFER)) != -1)
{
dest.write(data, 0, count);
}

this.files++;
dest.flush();
dest.close();
is.close();
}
zipfile.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* 遍历要压缩的目录及所有子目录
* @param inputUrl
* @param files
* @param out
* @param data
*/
public void compressFiles(String inputUrl, File[] files,
ZipOutputStream out,
byte data[])
{
try
{
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
{
String dirPath = inputUrl + files[i].getName() + "/";
File df = new File(dirPath);
File dirFiles[] = df.listFiles();
compressFiles(dirPath, dirFiles, out, data);
}
else
{
doZip(inputUrl, files[i], out, data);
}
}
}
catch (Exception e)
{
System.out.println(e);
}
}

/**
* 打包压缩文件
* @param inputUrl
* @param files
* @param out
* @param data
*/
public void doZip(String inputUrl, File files, ZipOutputStream out,
byte data[])
{
try
{
FileInputStream fi = new FileInputStream(inputUrl + files.getName());
BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files.getName());
out.putNextEntry(entry);
int count;
while ( (count = origin.read(data, 0, BUFFER)) != -1)
{
out.write(data, 0, count);
}
fi.close();
origin.close();
}
catch (IOException e)
{
System.out.println(e);
}
}

public int getFiles()
{
return this.files;
}

static public void main(String args[])
{
try
{
ZipFiles_20091120 zf = new ZipFiles_20091120();
// zf.zip("g:/test/", "g:\\test\\temp.zip");
/*
String tempFile="g:\\test\\temp.zip";
File file = new File(tempFile);
System.out.println((int) file.length());

byte[] buffer = new byte[ (int) file.length()];

BufferedInputStream input = new BufferedInputStream(new FileInputStream(tempFile));
input.read(buffer, 0, buffer.length);
input.close();
long b = System.currentTimeMillis();
System.out.println(buffer);

com.sti.datacenter.server.DataControlService ds = new com.sti.datacenter.server.DataControlService();
ds.sendClientData(buffer);
*/
zf.unZip("D:/temp/zipmobile.zip","D:/temp/");
System.out.println("OOOOOOOOOKKKKKKKKKK");
}
catch (Exception e)
{

}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值