|
@@ -87,79 +87,88 @@ public class FileUtils {
|
|
|
/**
|
|
|
* 文件压缩
|
|
|
*
|
|
|
- * @param target 目录或文件
|
|
|
- * @param zipFile 压缩后的ZIP文件
|
|
|
+ * @param target 待压缩的目录或文件
|
|
|
+ * @param zipFile 压缩后的文件
|
|
|
*/
|
|
|
- public static boolean doZip(File target, File zipFile) {
|
|
|
+ public static boolean zip(File target, File zipFile) {
|
|
|
if (target == null || !target.exists()) {
|
|
|
log.error("目录或文件不能为空!");
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
if (zipFile == null) {
|
|
|
log.error("待压缩的文件不能为空!");
|
|
|
return false;
|
|
|
}
|
|
|
- ZipOutputStream zipOutStream = null;
|
|
|
+
|
|
|
+ ZipOutputStream zipStream = null;
|
|
|
OutputStream outStream = null;
|
|
|
try {
|
|
|
if (!zipFile.exists()) {
|
|
|
zipFile.createNewFile();
|
|
|
}
|
|
|
+
|
|
|
outStream = new FileOutputStream(zipFile);
|
|
|
- zipOutStream = new ZipOutputStream(outStream, Charset.forName("UTF-8"));
|
|
|
+ zipStream = new ZipOutputStream(outStream, Charset.forName("UTF-8"));
|
|
|
+
|
|
|
if (target.isDirectory()) {
|
|
|
File[] files = target.listFiles();
|
|
|
if (files.length == 0) {
|
|
|
log.error("文件夹内未找到任何文件!");
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
for (File file : files) {
|
|
|
- doZip(zipOutStream, file, null);
|
|
|
+ doZip(zipStream, file, null);
|
|
|
}
|
|
|
} else {
|
|
|
- doZip(zipOutStream, target, null);
|
|
|
+ doZip(zipStream, target, null);
|
|
|
}
|
|
|
+ return true;
|
|
|
} catch (IOException e) {
|
|
|
log.error(e.getMessage(), e);
|
|
|
+ return false;
|
|
|
} finally {
|
|
|
- if (zipOutStream != null) {
|
|
|
- IOUtils.closeQuietly(zipOutStream);
|
|
|
- }
|
|
|
- if (outStream != null) {
|
|
|
- IOUtils.closeQuietly(outStream);
|
|
|
+ try {
|
|
|
+ if (zipStream != null) {
|
|
|
+ zipStream.close();
|
|
|
+ }
|
|
|
+ if (outStream != null) {
|
|
|
+ outStream.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ //ignore
|
|
|
}
|
|
|
}
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
- private static void doZip(ZipOutputStream zipOutStream, File target, String parentDir) throws IOException {
|
|
|
- //log.info("Zip:" + parentDir);
|
|
|
- if (parentDir == null) {
|
|
|
- parentDir = "";
|
|
|
- }
|
|
|
- if (!"".equals(parentDir) && !parentDir.endsWith(File.separator)) {
|
|
|
- parentDir += File.separator;
|
|
|
- }
|
|
|
- if (target.isDirectory()) {
|
|
|
- File[] files = target.listFiles();
|
|
|
+ private static void doZip(ZipOutputStream zipStream, File curFile, String dirName) throws IOException {
|
|
|
+ if (dirName == null) dirName = "";
|
|
|
+ String entryName = dirName + curFile.getName();
|
|
|
+ //log.debug("entryName:" + entryName);
|
|
|
+
|
|
|
+ if (curFile.isDirectory()) {
|
|
|
+ String subDirName = entryName + File.separator;
|
|
|
+ File[] files = curFile.listFiles();
|
|
|
if (files.length > 0) {
|
|
|
for (File file : files) {
|
|
|
- doZip(zipOutStream, file, parentDir + target.getName());
|
|
|
+ doZip(zipStream, file, subDirName);
|
|
|
}
|
|
|
} else {
|
|
|
- zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
|
|
|
- zipOutStream.closeEntry();
|
|
|
+ //空文件夹暂不处理
|
|
|
+ //zipStream.putNextEntry(new ZipEntry(subDirName));
|
|
|
+ //zipStream.closeEntry();
|
|
|
}
|
|
|
} else {
|
|
|
- InputStream is = new FileInputStream(target);
|
|
|
- zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
|
|
|
- int len = 0;
|
|
|
+ int len;
|
|
|
byte[] bytes = new byte[1024];
|
|
|
+ InputStream is = new FileInputStream(curFile);
|
|
|
+ zipStream.putNextEntry(new ZipEntry(entryName));
|
|
|
while ((len = is.read(bytes)) > 0) {
|
|
|
- zipOutStream.write(bytes, 0, len);
|
|
|
+ zipStream.write(bytes, 0, len);
|
|
|
}
|
|
|
IOUtils.closeQuietly(is);
|
|
|
- zipOutStream.closeEntry();
|
|
|
+ zipStream.closeEntry();
|
|
|
}
|
|
|
}
|
|
|
|