package com.sinotrans.filesystem.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class CustomizationZipUtil {
protected static final Log log = LogFactory
.getLog(CustomizationZipUtil.class);
/**
* @描述 压缩文件
* @param srcFiles
* 压缩路径
* @param zipPath
* 目标全路径
*/
public static void zipFileWithTier(String srcFiles, String zipPath) {
try {
FileOutputStream zipFile = new FileOutputStream(zipPath);
BufferedOutputStream buffer = new BufferedOutputStream(zipFile);
ZipOutputStream out = new ZipOutputStream(buffer);
zipFiles(srcFiles, out, "");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("", e);
}
}
public static void zipFiles(String filePath, ZipOutputStream out,
String prefix) throws IOException {
File file = new File(filePath);
if (file.isDirectory()) {
if (file.listFiles().length == 0) {
ZipEntry zipEntry = new ZipEntry(prefix + file.getName() + "/");
out.putNextEntry(zipEntry);
out.closeEntry();
} else {
prefix += file.getName() + File.separator;
for (File f : file.listFiles())
zipFiles(f.getAbsolutePath(), out, prefix);
}
} else {
FileInputStream in = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(prefix + file.getName());
out.putNextEntry(zipEntry);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
}
/**
* @描述 解压缩文件
* @param zipFilePath
* 待解压文件路径
* @param prefix
* 目标路径
* @throws IOException
*/
public static void unzipFilesWithTier(String zipFilePath, String prefix)
throws IOException {
byte[] bytes = readFileByte(zipFilePath);
prefix = prefix + File.separator;
InputStream bais = new ByteArrayInputStream(bytes);
ZipInputStream zin = new ZipInputStream(bais);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
File file = new File(prefix + ze.getName());
if (!file.exists())
file.mkdirs();
continue;
}
File file = new File(prefix + ze.getName());
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
ByteArrayOutputStream toScan = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = zin.read(buf)) > 0) {
toScan.write(buf, 0, len);
}
byte[] fileOut = toScan.toByteArray();
toScan.close();
writeByteFile(fileOut, new File(prefix + ze.getName()));
}
zin.close();
bais.close();
}
@SuppressWarnings("resource")
public static byte[] readFileByte(String filename) throws IOException {
if (filename == null || filename.equals("")) {
throw new NullPointerException("File is not exist!");
}
File file = new File(filename);
long len = file.length();
byte[] bytes = new byte[(int) len];
BufferedInputStream bufferedInputStream = new BufferedInputStream(
new FileInputStream(file));
int r = bufferedInputStream.read(bytes);
if (r != len)
throw new IOException("Read file failure!");
bufferedInputStream.close();
return bytes;
}
public static String writeByteFile(byte[] bytes, File file) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(bytes);
} catch (FileNotFoundException e) {
log.error("", e);
} catch (IOException e) {
log.error("", e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
log.error("", e);
}
}
}
return "success";
}
}
文件压缩工具类
最新推荐文章于 2024-05-10 09:46:44 发布