[Java]zip压缩文件、文件夹并保存成文件

2021-07-01 963点热度 0人点赞 0条评论

示例Java代码把多个文件用zip格式压缩成ByteArrayOutputStream并存成文件:

public static void main(String[] args) throws Exception {
//压缩doc下所有的文件
File[] files = new File("/Users/Terry/Downloads/doc").listFiles();
assert files != null;
ByteArrayOutputStream bos = zipFiles(files);
File zipFile = new File("/Users/Terry/Downloads/test.zip");
//try with resource的方式,把ByteArrayOutputStream存成文件
try (OutputStream outputStream = new FileOutputStream(zipFile)) {
bos.writeTo(outputStream);
}
}
/**
* 打包文件list成ByteArrayOutputStream
*/
private static ByteArrayOutputStream zipFiles(File... files) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(bos);
for (File file : files) {
if (file != null) {
try {
addZipEntry(zos, file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
zos.close();
return bos;
}
/**
* 把单个文件压缩到加入zos里
*/
private static void addZipEntry(ZipOutputStream zos, File file) throws Exception {
byte[] buffer = new byte[4096];
String fileName = file.getName(); //压缩包里的文件名,这里取的是文件的文件名,也可以自定义
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
FileInputStream fis = new FileInputStream(file);
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
fis.close();
zos.closeEntry();
}
public static void main(String[] args) throws Exception { //压缩doc下所有的文件 File[] files = new File("/Users/Terry/Downloads/doc").listFiles(); assert files != null; ByteArrayOutputStream bos = zipFiles(files); File zipFile = new File("/Users/Terry/Downloads/test.zip"); //try with resource的方式,把ByteArrayOutputStream存成文件 try (OutputStream outputStream = new FileOutputStream(zipFile)) { bos.writeTo(outputStream); } } /** * 打包文件list成ByteArrayOutputStream */ private static ByteArrayOutputStream zipFiles(File... files) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(bos); for (File file : files) { if (file != null) { try { addZipEntry(zos, file); } catch (Exception e) { e.printStackTrace(); } } } zos.close(); return bos; } /** * 把单个文件压缩到加入zos里 */ private static void addZipEntry(ZipOutputStream zos, File file) throws Exception { byte[] buffer = new byte[4096]; String fileName = file.getName(); //压缩包里的文件名,这里取的是文件的文件名,也可以自定义 ZipEntry zipEntry = new ZipEntry(fileName); zos.putNextEntry(zipEntry); FileInputStream fis = new FileInputStream(file); int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } fis.close(); zos.closeEntry(); }
public static void main(String[] args) throws Exception {
    //压缩doc下所有的文件
    File[] files = new File("/Users/Terry/Downloads/doc").listFiles();
    assert files != null;
    ByteArrayOutputStream bos = zipFiles(files);
    File zipFile = new File("/Users/Terry/Downloads/test.zip");
    //try with resource的方式,把ByteArrayOutputStream存成文件
    try (OutputStream outputStream = new FileOutputStream(zipFile)) {
        bos.writeTo(outputStream);
    }

}

/**
 * 打包文件list成ByteArrayOutputStream
 */
private static ByteArrayOutputStream zipFiles(File... files) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(bos);
    for (File file : files) {
        if (file != null) {
            try {
                addZipEntry(zos, file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    zos.close();
    return bos;
}

/**
 * 把单个文件压缩到加入zos里
 */
private static void addZipEntry(ZipOutputStream zos, File file) throws Exception {
    byte[] buffer = new byte[4096];
    String fileName = file.getName(); //压缩包里的文件名,这里取的是文件的文件名,也可以自定义
    ZipEntry zipEntry = new ZipEntry(fileName);
    zos.putNextEntry(zipEntry);
    FileInputStream fis = new FileInputStream(file);
    int len;
    while ((len = fis.read(buffer)) > 0) {
        zos.write(buffer, 0, len);
    }
    fis.close();
    zos.closeEntry();
}

如何使用Java解压缩文件,请看:https://blog.terrynow.com/2021/07/02/java-unzip-file-how-to/

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论