文件的压缩和解压

/**
*
*/
package com.oooo3d.zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
* @author bob
* @创建时间:2013-1-16下午01:44:13
* @version 2.0
*/
public class ZipOrUzipFile
{

//private static String encoding="UTF-8";

public ZipOrUzipFile()
{
}

/**
* 文件压缩
*
* @param inputFilename
* 压缩的目标文件
* @param zipFilename
* 压缩后的文件
* @throws IOException
*/
public void zip(String inputFilename, String zipFilename)
throws IOException
{
zip(new File(inputFilename), zipFilename);
}

public void zip(File inputFile, String zipFilename) throws IOException
{
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(zipFilename),1024));


try
{
zip(inputFile, out, "");
}
catch (IOException e)
{
throw e;
}
finally
{
out.close();
}
}

/**
* 压缩
*
* @param inputFile
* @param out
* @param str
* @throws IOException
* @see [类、类#方法、类#成员]
*/
private void zip(File inputFile, ZipOutputStream out, String str)
throws IOException
{

if (inputFile.isDirectory())
{
File[] inputFiles = inputFile.listFiles();
out.putNextEntry(new ZipEntry(str + "/"));
str = str.length() == 0 ? "" : str + "/";
int len = inputFiles.length;
for (int i = 0; i < len; i++)
{
zip(inputFiles[i], out, str + inputFiles[i].getName());
}
}
else
{
//str = new String(str.getBytes("ISO-8859-1"),"GBK");
if (str.length() > 0)
{
out.putNextEntry(new ZipEntry(str));
}
else
{
out.putNextEntry(new ZipEntry(inputFile.getName()));
}

// 字节读取流
//FileInputStream in = new FileInputStream(inputFile);
DataInputStream d = new DataInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
try
{
int c;
byte[] ch = new byte[1024];
while ((c = d.read(ch)) != -1)
{
out.write(ch, 0, c);
}

}
catch (IOException e)
{
throw e;
}
finally
{
d.close();
}
}
}

/**
* 解压
*
* @param zipFilename
* 需要解压的文件
* @param outputDirectory
* 解压后的文件目录
* @throws IOException
* @see [类、类#方法、类#成员]
*/
@SuppressWarnings("unchecked")
public void unzip(String zipFilename, String outputDirectory)
throws IOException
{
File outFile = new File(outputDirectory);
if (!outFile.exists())
{
// 如果不存在则创建目录
outFile.mkdirs();
}

ZipFile zipFile = new ZipFile(zipFilename);
Enumeration<ZipEntry> en = (Enumeration<ZipEntry>) zipFile.entries();
ZipEntry zipEntry = null;
while (en.hasMoreElements())
{
zipEntry = (ZipEntry) en.nextElement();
if (zipEntry.isDirectory())
{
String dirName = zipEntry.getName();
dirName = dirName.substring(0, dirName.length() - 1);

File f = new File(outFile.getPath() + File.separator + dirName);
f.mkdirs();

}
else
{
File f = new File(outFile.getPath() + File.separator
+ zipEntry.getName());

// 创建新的文件
f.createNewFile();

InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out = new FileOutputStream(f);

try
{
int c;
byte[] by = new byte[1024];
while ((c = in.read(by)) != -1)
{
out.write(by, 0, c);
}
}
catch (IOException e)
{
throw e;
}
finally
{
out.close();
in.close();
}
}
}
}

public static void main(String[] args)
{
ZipOrUzipFile compfile = new ZipOrUzipFile();
try
{
// 压缩
compfile.zip("D:/aa/bb", "D:/aa/bb.zip");

// 解压
compfile.unzip("D:/aa/bb.zip", "D:/aa/dd");

}
catch (IOException e)
{
e.printStackTrace();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值