1、压缩文件
logger.info("Start to compress!");
File file = new File("C:/1.csv");
String zipFileName = "C:/test.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
out.setMethod(ZipOutputStream.DEFLATED);
out.putNextEntry(new ZipEntry("1.csv"));
FileInputStream in = new FileInputStream(file);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
logger.info("End compressing!");
2、压缩目录
public class TestZip {
public static String SERPEROT = "/";
public static int BUFFER = 2048;
public static void main(String args[]) {
zip("e:/hello/", "e:/hello.zip");
}
public static void zip(String srcFile, String descFile) {
ZipOutputStream zos = null;
FileOutputStream fos = null;
File file = null;
try {
fos = new FileOutputStream(descFile);
zos = new ZipOutputStream(fos);
file = new File(srcFile);
String folder = srcFile.substring(srcFile.lastIndexOf("/") + 1, srcFile.length());
zip(zos, file, folder);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (zos != null) {
zos.close();
}
if (fos != null) {
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void zip(ZipOutputStream descFile, File srcFile, String srcfolder) {
FileInputStream fis = null;
System.out.println(srcFile.isDirectory());
try {
if (srcFile.isDirectory()) {
File[] files = srcFile.listFiles();
descFile.putNextEntry(new ZipEntry(srcfolder + "/")); // 是压缩包里面的路径.
srcfolder = srcfolder.length() == 0 ? "" : srcfolder + "/";
System.out.println(srcfolder);
for (int i = 0; i < files.length; i++) {
zip(descFile, files[i], srcfolder + files[i].getName());
}
} else {
descFile.putNextEntry(new ZipEntry(srcfolder));
fis = new FileInputStream(srcFile);
byte[] bytes = new byte[2048];
int n = 0;
while ((n = fis.read(bytes)) != -1) {
descFile.write(bytes, 0, n);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
3. 解压目录及文件
解压缩与压缩运作方式相反,原理大抵相同,由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码
privatevoid unzipFile(InputStream in)throws IOException {
finalint bufferSize = 1024;
ZipInputStream zip = null;
BufferedOutputStream bos = null;
try {
CheckedInputStream cis =new CheckedInputStream(in,new CRC32());
zip = new ZipInputStream(cis);
ZipEntry entry = null;
while ((entry = zip.getNextEntry()) !=null) {
StringBuilder entryPath =new StringBuilder(PathConstants.SALT_FILE_ROOT);
entryPath.append(entry.getName());
File entryFile = new File(entryPath.toString());
fileProber(entryFile);
if (entry.isDirectory()) {
entryFile.mkdir();
} else {
bos = new BufferedOutputStream(new FileOutputStream(entryFile));
int count;
byte data[] =newbyte[bufferSize];
while ((count = zip.read(data, 0, bufferSize)) != -1) {
bos.write(data, 0, count);
}
bos.close();
}
zip.closeEntry();
}
} finally {
if (zip !=null) {
try {
zip.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
if (bos !=null) {
try {
bos.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
}
privatevoid fileProber(File dirFile) {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) {
fileProber(parentFile);
parentFile.mkdir();
}
}