您的位置:首页 > 编程语言 > Java开发

Java 解压和压缩文件(文件夹)

2012-12-15 16:23 555 查看
package notion.touch.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import notion.basic.vo.exception.BusinessException;

/**
* 压缩文件夹公共类
* @author ganxz
* @date 2012-12-19
*
*/
public class ZipUtils {
/**
* 文件夹压缩公共方法
* @param inputdir  要压缩的文件夹路径
* @param zipFileName  要导出压缩文件的路径
* @throws Exception
*/
public static void zipFolder(String inputdir,String zipFileName) throws Exception {
zipFileName = zipFileName.replace("/", "\\");
String zipdir = zipFileName.substring(0,zipFileName.lastIndexOf("\\"));
File zipDirFile = new File(zipdir);
if(!zipDirFile.exists())
zipDirFile.mkdirs();
zip(zipFileName, new File(inputdir));
}

private static  void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "");
System.out.println("压缩完成");
out.close();
}

private static void zip(ZipOutputStream out, File f, String base) throws Exception {
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;
System.out.println(base);
while ( (b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}

/**
* 压缩文件
* @param filepath 要压缩的文件路径
* @param zipname  要导出的压缩文件路径
* @throws Exception
*/
public static void zipFile(String filepath,String zipname) throws Exception{

File f = new File(filepath);
if(f.exists())
throw new Exception("要压缩的文件不存在,请检查!");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] buf = new byte[1024];
int len;
if(zipname == null || zipname == "" ){
zipname = f.getName()+".zip";
}else{
if(!zipname.substring(zipname.indexOf(".")).equals(".zip"))
zipname += ".zip";
}

FileOutputStream fos = new FileOutputStream(zipname);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);//压缩包
ZipEntry ze = new ZipEntry(f.getName());//这是压缩包名里的文件名
zos.putNextEntry(ze);//写入新的 ZIP 文件条目并将流定位到条目数据的开始处

while((len=bis.read(buf))!=-1)
{
zos.write(buf,0,len);
zos.flush();
}
bis.close();
zos.close();
}

/**
* 解压  压缩文件
* @param zipFileName 压缩文件的路径
* @param extPlace 解压文件夹路径
* @throws Exception
*/
public static void extZipFileList(String zipFileName, String extPlace) throws Exception {
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(
zipFileName));

ZipEntry entry = null;

while ((entry = in.getNextEntry()) != null) {

String entryName = entry.getName();
String dir = extPlace + entryName;
File f = new File(dir.substring(0,dir.lastIndexOf("/")));
if(!f.exists())
f.mkdirs();
if (entry.isDirectory()) {
File file = new File(extPlace + entryName);
file.mkdirs();
} else {

FileOutputStream os = new FileOutputStream(extPlace
+ entryName);

// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024];

int len;
while ((len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
in.closeEntry();
}
}

} catch (Exception e) {
throw new Exception("解压文件失败!原因:",e);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: