package com.spider.reader.common.tool;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import com.spider.reader.common.util.StringUtils;
public class ZipUtil {
/** 得到当前系统的分隔符 */
// private static String separator = System.getProperty("file.separator");
/**
* 添加到压缩文件中
*
* @param out
* @param f
* @param base
* @throws Exception
*/
private static void directoryZip(ZipOutputStream out, File f, String base) throws Exception {
// 如果传入的是目录
if (f.isDirectory()) {
File[] fl = f.listFiles();
// 创建压缩的子目录
out.putNextEntry(new ZipEntry(base + "/"));
if (base.length() == 0) {
base = "";
} else {
base = base + "/";
}
for (int i = 0; i < fl.length; i++) {
directoryZip(out, fl[i], base + fl[i].getName());
}
} else {
// 把压缩文件加入rar中
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
byte[] bb = new byte[10240];
int aa = 0;
while ((aa = in.read(bb)) != -1) {
out.write(bb, 0, aa);
}
in.close();
}
}
/**
* 压缩文件
*
* @param zos
* @param file
* @throws Exception
*/
private static void fileZip(ZipOutputStream zos, File file) throws Exception {
if (file.isFile()) {
zos.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fis = new FileInputStream(file);
byte[] bb = new byte[10240];
int aa = 0;
while ((aa = fis.read(bb)) != -1) {
zos.write(bb, 0, aa);
}
fis.close();
} else {
directoryZip(zos, file, "");
}
}
/**
* 解压缩文件
*
* @param zis
* @param file
* @throws Exception
*/
// private void fileUnZip(ZipInputStream zis, File file) throws Exception {
// ZipEntry zip = zis.getNextEntry();
// if (zip == null)
// return;
// String name = zip.getName();
// File f = new File(file.getAbsolutePath() + "/" + name);
// if (zip.isDirectory()) {
// f.mkdirs();
// fileUnZip(zis, file);
// } else {
// f.createNewFile();
// FileOutputStream fos = new FileOutputStream(f);
// byte b[] = new byte[10240];
// int aa = 0;
// while ((aa = zis.read(b)) != -1) {
// fos.write(b, 0, aa);
// }
// fos.close();
// fileUnZip(zis, file);
// }
// }
private void fileUnZip(ZipFile zipFile, File file) throws Exception {
Enumeration<?> enums = zipFile.getEntries();
while(enums.hasMoreElements()){
ZipEntry zip = (ZipEntry) enums.nextElement();
if (zip == null)
return;
String name = zip.getName();
File f = new File(file.getAbsolutePath() + "/" + name);
if (zip.isDirectory()) {
f.mkdirs();
} else {
f.createNewFile();
InputStream is = null;
OutputStream os = null;
try {
is = zipFile.getInputStream(zip);
os = new FileOutputStream(f);
byte b[] = new byte[10240];
int aa = 0;
while ((aa = is.read(b)) != -1) {
os.write(b, 0, aa);
}
os.close();
} catch (IOException e) {
throw e;
}finally{
if(is!=null)
is.close();
if(os!=null)
os.close();
}
}
}
}
/**
* 根据filePath创建相应的目录
*
* @param filePath
* @return
* @throws IOException
*/
private static File mkdirFiles(String filePath) throws IOException {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
return file;
}
/**
* 对zipBeforeFile目录下的文件压缩,保存为指定的文件zipAfterFile
*
* @param zipBeforeFile
* 压缩之前的文件
* @param zipAfterFile
* 压缩之后的文件
*/
public static void zip(String mkdirName, String fileName) {
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(mkdirFiles(fileName)));
fileZip(zos, new File(mkdirName));
/** 设置编码方式,避免中文的文件名显示成乱码* */
zos.setEncoding("gbk");
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解压缩文件unZipBeforeFile保存在unZipAfterFile目录下
*
* @param unZipBeforeFile 解压之前的文件
* @param unZipAfterFile 解压之后的文件
*/
// public void unZip(String unZipBeforeFile, String unZipAfterFile) {
// try {
// ZipInputStream zis = new ZipInputStream(new FileInputStream(unZipBeforeFile));
// File f = new File(unZipAfterFile);
// f.mkdirs();
// fileUnZip(zis, f);
// zis.close();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
public void unZip(String unZipBeforeFile, String unZipAfterFile) {
try {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(unZipBeforeFile);
File f = new File(unZipAfterFile);
f.mkdirs();
fileUnZip(zipFile, f);
} catch (IOException e) {
throw new IOException("解压缩文件出现异常");
}finally{
try{
if(zipFile != null){
zipFile.close();
}
}catch(IOException ex){
throw new IOException("关闭zipFile出现异常");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copy(String path1, String path2) throws IOException {
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(path1)));
byte[] date = new byte[in.available()];
in.read(date);
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path2)));
out.write(date);
in.close();
out.close();
}
/**
* 拷贝图片 新图片以流的形式存储
* @param path1
* @param path2
* @throws IOException
*/
public static void copyStream(String path1, String path2) throws IOException{
if(StringUtils.isNotEmpty(path2) && path2.contains(".")){
path2 = path2.substring(0, path2.lastIndexOf("."));
File fileNew = new File(path1);
File fileOld = new File(path2);
InputStream input = null;
FileOutputStream output = null;
try{
input = new FileInputStream(fileNew);
output = new FileOutputStream(fileOld);
FileInputStream in = new FileInputStream(path1);
byte[] b = new byte[in.available()];
int read;
while ((read = input.read(b)) != -1) {
output.write(b, 0, read);
}
}finally{
if(null != input){
input.close();
}
if(null != output){
output.close();
}
}
}
}
/**
* 拷贝图片 新图片以流的形式存储
* @param path1
* @param path2
* @throws IOException
*/
public static void copyStreamTwo(String path1, String path2) {
File fileNew = new File(path1);
File fileOld = new File(path2);
InputStream input = null;
FileOutputStream output = null;
try{
input = new FileInputStream(fileNew);
output = new FileOutputStream(fileOld);
FileInputStream in = new FileInputStream(path1);
byte[] b = new byte[in.available()];
int read;
while ((read = input.read(b)) != -1) {
output.write(b, 0, read);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(null != input){
input.close();
}
if(null != output){
output.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}