java简单实现文件夹及文件压缩和解压
前言
网上对文件的操作方法有很多,但多文件夹操作的却很少,最近项目上刚好遇到对文件夹进行导入导出的需求,实现方案为将文件夹以压缩包的形式导出,导入时也以压缩包导入,代码实现压缩包的解压操作。
代码
package com.example.demotest.utils;
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.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ZipUtils {
private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);
/**
* 压缩文件
*
* @param srcfile
* File[] 需要压缩的文件列表 "F:\\asd" * @param zipfile * File 压缩后的文件
* "f:\\qwe_16_V6.0.0_1.zip";
* @throws IOException
* * @throws FileNotFoundException
*/
public static void zipFiles(File[] srcfile, File zipfile) throws IOException {
ZipOutputStream zipOut = null;
try {
if (!zipfile.getParentFile().exists())
zipfile.getParentFile().mkdirs();
if (!zipfile.exists())
zipfile.createNewFile();
zipOut = new ZipOutputStream(new FileOutputStream(zipfile));
for (int i = 0; i < srcfile.length; ++i) {
if (srcfile[i].isDirectory()) {
zipDir(zipOut, srcfile[i], srcfile[i].getName());
} else {
zipFile(zipOut, srcfile[i], null);
}
}
} catch (Exception e1) {
logger.warn("close error {}", e1.getMessage());
} finally {
try {
zipOut.close();
} catch (Exception e) {
logger.warn("close error {}", e.getMessage());
}
}
}
/**
*
* 递归压缩文件夹 faterName的目的就是为了维持原来的目录 *
*
* @param zipOut
* @param dirFile
* @param faterName
* * @return
* @throws IOException
*/
private static ZipOutputStream zipDir(ZipOutputStream zipOut, File dirFile, String faterName) throws IOException {
File[] fileList = dirFile.listFiles();
if (fileList.length > 0) {
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
zipDir(zipOut, fileList[i], faterName + File.separator + fileList[i].getName());
} else {
zipFile(zipOut, fileList[i], faterName);
}
}
} else {
zipOut.putNextEntry(new ZipEntry(faterName + "/"));
}
return zipOut;
}
/**
* 压缩单个文件
*
* @param zipOut
* @param file
* @throws IOException
*/
private static void zipFile(ZipOutputStream zipOut, File file, String faterName) throws IOException {
InputStream input = null;
try {
byte[] buf = new byte[1024];
input = new FileInputStream(file);
if (faterName != null) {
zipOut.putNextEntry(new ZipEntry(faterName + File.separator + file.getName()));
} else {
zipOut.putNextEntry(new ZipEntry(File.separator + file.getName()));
}
int temp = 0;
while ((temp = input.read(buf)) != -1) {
zipOut.write(buf, 0, temp);
}
} catch (Exception e1) {
logger.warn("zipFile error {}", e1.getMessage());
} finally {
try {
if (input != null) {
input.close();
}
} catch (Exception e) {
logger.warn("zipFile close error {}", e.getMessage());
}
}
}
/**
* zip解压缩 *
*
* @param zipfile
* File 需要解压缩的文件 "f:\\qwe_16_V6.0.0_1.zip";
* @param descDir
* * String 解压后的目标目录 "F:\\asd";
*/
public static void unzipFiles(File file, String outputDirectory) throws IOException {
unzipFiles(file, outputDirectory, false);
}
/**
*
* @param file
* @param outputDirectory
* @param currentDir
* @throws IOException
* /var/app/resource/app5.zip outputDirectory /var/app/resource/
* currentDir true --->/var/app/resource/app5/xxx.txt currentDir
* false --->/var/app/resource/xxx.txt
*/
public static void unzipFiles(File file, String outputDirectory, boolean currentDir) throws IOException {
File outzipFile = new File(outputDirectory);
if (!outzipFile.exists()) {
if (!outzipFile.mkdirs()) {
return;
}
}
byte[] buf = new byte[1024];
File outFile = null;
ZipFile zipFile = null;
ZipInputStream zipInput = null;
ZipEntry entry = null;
InputStream input = null;
OutputStream output = null;
try {
zipFile = new ZipFile(file);
zipInput = new ZipInputStream(new FileInputStream(file));
while ((entry = zipInput.getNextEntry()) != null) {
if (currentDir) {
outFile = new File(outputDirectory + entry.getName());
} else {
outFile = new File(outputDirectory + file.getName().substring(0, file.getName().lastIndexOf("."))
+ File.separator + entry.getName());
}
if (entry.isDirectory()) {
if (!outFile.mkdirs()) {
continue;
}
} else {
if (!outFile.getParentFile().exists()) {
if (!outFile.getParentFile().mkdirs()) {
continue;
}
}
if (!outFile.createNewFile()) {
continue;
}
try {
input = zipFile.getInputStream(entry);
output = new FileOutputStream(outFile);
int temp = 0;
while ((temp = input.read(buf)) != -1) {
output.write(buf, 0, temp);
}
} catch (Exception e1) {
logger.warn("close error {}", e1.getMessage());
} finally {
try {
if (input != null) {
input.close();
}
} catch (Exception e) {
logger.warn("close error {}", e.getMessage());
}
try {
if (output != null) {
output.close();
}
} catch (Exception e) {
logger.warn("close error {}", e.getMessage());
}
}
}
}
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (Exception e) {
logger.warn("close error {}", e.getMessage());
}
try {
if (zipInput != null) {
zipInput.close();
}
} catch (Exception e) {
logger.warn("close error {}", e.getMessage());
}
}
}
}