1.将单个文件打成压缩包
需求:
将E盘下的 test.xls 文件打成压缩包保存到E盘目录下的 target.zip 文件中
代码实现:ZipUtil.java
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* <p> @Title ZipUtil
* <p> @Description 将单个文件打成压缩包
*
* @author ACGkaka
* @date 2020/9/15 14:11
*/
public class ZipUtil {
public static void main(String[] args) throws Exception {
String dir = "E:\\test.xls";
String zip = "E:\\target.zip";
zip(dir, zip);
}
/**
* 打包
*
* @param dir 要打包的目录
* @param zipFile 打包后的文件路径
* @throws Exception
*/
public static void zip(String dir, String zipFile) throws Exception {
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile))) {
File sourceFile = new File(dir);
out.putNextEntry(new ZipEntry(sourceFile.getName()));
try (FileInputStream in = new FileInputStream(sourceFile)) {
IOUtils.copy(in, out);
} catch (Exception e) {
throw new RuntimeException("打包异常: " + e.getMessage());
}
}
}
}
2.将E:\zip目录打成压缩包
需求:
将E盘zip文件夹下面的内容打成压缩包保存到E盘目录下的 target.zip 文件中
代码实现:ZipUtil.java
import java.io.FileOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import java.io.File;
import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* <p> @Title ZipUtil
* <p> @Description 文件夹打压缩包
*
* @author ACGkaka
* @date 2020/9/15 13:48
*/
public class ZipUtil {
public static void main(String[] args) throws Exception {
String dir = "E:\\zip";
String zip = "E:\\target.zip";
String rar = "E:\\target.rar";
zip(dir, zip);
zip(dir, rar, true);
}
/**
* 打包
*
* @param dir 要打包的目录
* @param zipFile 打包后的文件路径
* @throws Exception
*/
public static void zip(String dir, String zipFile) throws Exception {
zip(dir, zipFile, false);
}
/**
* 打包
*
* @param dir 要打包的目录
* @param zipFile 打包后的文件路径
* @param includeBaseDir 是否包括最外层目录
* @throws Exception
*/
public static void zip(String dir, String zipFile, boolean includeBaseDir) throws Exception {
if (zipFile.startsWith(dir)) {
throw new RuntimeException("打包生成的文件不能在打包目录中");
}
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile))) {
File fileDir = new File(dir);
String baseDir = "";
if (includeBaseDir) {
baseDir = fileDir.getName();
}
compress(out, fileDir, baseDir);
}
}
public static void compress(ZipOutputStream out, File sourceFile, String base) throws Exception {
if (sourceFile.isDirectory()) {
base = base.length() == 0 ? "" : base + File.separator;
File[] files = sourceFile.listFiles();
if (ArrayUtils.isEmpty(files)) {
// todo 打包空目录
// out.putNextEntry(new ZipEntry(base));
return;
}
for (File file : files) {
compress(out, file, base + file.getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
try (FileInputStream in = new FileInputStream(sourceFile)) {
IOUtils.copy(in, out);
} catch (Exception e) {
throw new RuntimeException("打包异常: " + e.getMessage());
}
}
}
}
参考博客:https://blog.csdn.net/frankcheng5143/article/details/105129108