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

java压缩解压文件(代码记录)

2017-05-22 12:12 411 查看
package com.train.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

/**
* @author Feng
*
*         文件编码解压方式
*
*/
public class MyZipUtils {

/**
* 根据不同类型编码解压
* @param fileAddress
*            zip文件路径
* @param unZipAddress
*            zip文件解压地址
* @throws Exception
*/
public static void unZipFiles(String fileAddress, String unZipAddress)
throws Exception {

File file = new File(fileAddress);
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
System.out.println("解压文件不存在!");
}
Enumeration<ZipEntry> e = zipFile.getEntries();
while (e.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) e.nextElement();
if (zipEntry.isDirectory()) {
// 文件名称
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(unZipAddress + name);
f.mkdirs();
} else {
File f = new File(unZipAddress + zipEntry.getName());
f.getParentFile().mkdirs();
f.createNewFile();
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(f);
fos.toString().getBytes("UTF-8");
int length = 0;
byte[] b = new byte[1024];
while ((length = is.read(b, 0, 1024)) != -1) {
fos.write(b, 0, length);
}
//因本身文件写入方式为gbk,web展示出现乱码,故重新读取写入utf-8
String name = zipEntry.getName();
if(name.substring(name.lastIndexOf(".")+1).equals("html")||name.substring(name.lastIndexOf(".")+1).equals("htm")){
InputStreamReader isr = new InputStreamReader(zipFile.getInputStream(zipEntry), "GBK");
StringBuffer sbread = new StringBuffer();
while (isr.ready()) {
sbread.append((char) isr.read());
}
isr.close();
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(f),"UTF-8");
out.write(sbread.toString().toCharArray());
out.flush();
out.close();
}
is.close();
fos.close();
}
}
if (zipFile != null) {
zipFile.close();
}
file.delete();
}

/**
* 压缩文件
* sourcePath 需要压缩的路径
* zipFileName 压缩包名
*/
public static void ZIP(String sourcePath, String zipFileName)
throws IOException {
ZipOutputStream zos = new ZipOutputStream(new File(zipFileName));
// 设置压缩的时候文件名编码为gb2312
zos.setEncoding("gb2312");
File file = new File(sourcePath);
if (file.isDirectory()) {
// 此处使用/来表示目录,如果使用\\来表示目录的话,有可能导致压缩后的文件目录组织形式在解压缩的时候不能正确识别。
ZIPDIR(sourcePath, zos, file.getName() + "/");
} else {
// 如果直接压缩文件
ZIPDIR(file.getPath(), zos, new File(file.getParent()).getName()
+ "/");
ZIPFile(file.getPath(), zos, new File(file.getParent()).getName()
+ "/" + file.getName());
}
zos.closeEntry();
zos.close();
}

public static void ZIPDIR(String sourceDir, ZipOutputStream zos,
String tager) throws IOException {
// System.out.println(tager);
ZipEntry ze = new ZipEntry(tager);
zos.putNextEntry(ze);
// 提取要压缩的文件夹中的所有文件
File f = new File(sourceDir);
File[] flist = f.listFiles();
if (flist != null) {
// 如果该文件夹下有文件则提取所有的文件进行压缩
for (File fsub : flist) {
if (fsub.isDirectory()) {
// 如果是目录则进行目录压缩
ZIPDIR(fsub.getPath(), zos, tager + fsub.getName() + "/");
} else {
// 如果是文件,则进行文件压缩
ZIPFile(fsub.getPath(), zos, tager + fsub.getName());
}
}
}
}

public static void ZIPFile(String sourceFileName, ZipOutputStream zos,
String tager) throws IOException {
// System.out.println(tager);
ZipEntry ze = new ZipEntry(tager);
zos.putNextEntry(ze);
// 读取要压缩文件并将其添加到压缩文件中
FileInputStream fis = new FileInputStream(new File(sourceFileName));
byte[] bf = new byte[2048];
int location = 0;
while ((location = fis.read(bf)) != -1) {
zos.write(bf, 0, location);
}
fis.close();
}

/**
* 解压zip
* sourceFileName 压缩包路径
* desDir 解压的路径
*/
public static void UnZIP(String sourceFileName, String desDir)
throws IOException {
File file = new File(sourceFileName);
// 创建压缩文件对象
ZipFile zf = new ZipFile(file, "GBK");
// 获取压缩文件中的文件枚举
Enumeration<ZipEntry> en = zf.getEntries();
int length = 0;
byte[] b = new byte[1024];
// 提取压缩文件夹中的所有压缩实例对象
while (en.hasMoreElements()) {
ZipEntry ze = (ZipEntry) en.nextElement();
// System.out.println("压缩文件夹中的内容:"+ze.getName());
// System.out.println("是否是文件夹:"+ze.isDirectory());
// 创建解压缩后的文件实例对象
File f = new File(desDir + ze.getName());
// System.out.println("解压后的内容:"+f.getPath());
// System.out.println("是否是文件夹:"+f.isDirectory());
// 如果当前压缩文件中的实例对象是文件夹就在解压缩后的文件夹中创建该文件夹
if (f.isDirectory()) {
f.mkdirs();
} else {
// 如果当前解压缩文件的父级文件夹没有创建的话,则创建好父级文件夹
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
// 将当前文件的内容写入解压后的文件夹中。
FileOutputStream outputStream = new FileOutputStream(f);
InputStream inputStream = zf.getInputStream(ze);
while ((length = inputStream.read(b)) > 0) {
outputStream.write(b, 0, length);
}
inputStream.close();
outputStream.close();
}
}
zf.close();
file.delete();
}
}



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