Java的文件压缩-Zip格式

红烧土豆泥
• 阅读 1226
/**
  * 文件压缩
  */
public class ZipFile_Compress {
    public static void main(String[] args) throws IOException {
        File file = new File("E:\\aaa\\web01");

        String savePath = "E:\\aaa" + File.separator + file.getName() + ".zip";
        File saveFile = new File(savePath);
        if (!saveFile.getParentFile().exists()) {
            saveFile.getParentFile().mkdirs();
        }

        ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(savePath, true));

        toMakeZipFilePre(file, saveFile.getParentFile().getPath(), outputStream);

        outputStream.close();
    }

    /**
     * 文件压缩前的准备处理
     * @param file
     * @param savePath
     * @param outputStream
     * @throws IOException
     */
    public static void toMakeZipFilePre(File file, String savePath, ZipOutputStream outputStream) throws IOException {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    toMakeZipFilePre(f, savePath, outputStream);
                }
            }
        } else {
            /**
             * 截取后面要用的文件条目
             */
            String realSavePath = file.getAbsolutePath().substring(savePath.length() + 1).replaceAll("\\\\", "/");
            toMakeZipFile(file, realSavePath, outputStream);
            System.out.println(realSavePath);
        }
    }

    public static void toMakeZipFile(File file, String savePath, ZipOutputStream outputStream) throws IOException {
        /**
         * 将相对路径封装成条目
         */
        ZipEntry zipEntry = new ZipEntry(savePath);
        /**
         * 在输出流中编写一条新的条目
         */
        outputStream.putNextEntry(zipEntry);

        InputStream inputStream = new BufferedInputStream(new FileInputStream(file));

        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        outputStream.flush();
        inputStream.close();
    }

}
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
Java中zip的压缩和解压缩
在Java中可以使用ZipOutputStream和ZipInputStream来实现zip的压缩和解压缩操作,另外使用FileSystem也可以用来实现zip的解压缩,下面将介绍这几种方式,直接上代码。zip压缩待压缩文件目录结构:!(https://static.oschina.net/uploads/img/202102/2
Stella981 Stella981
2年前
Linux下创建加密的压缩文件
Linux下创建加密的压缩文件方法一:1.使用ZIP命令创建一个加密的ZIP文件:2.解压缩加密文件时,会提示要求输入密码:方法二:1.使用7z创建一个zip文件:2.解压缩加密文件:3.使用限制方法三:1.Tocre
Wesley13 Wesley13
2年前
JAR WAR EAR的使用和区别
1.JARWAREAR的使用和区别最近项目中接触到了JarWarEAR,在文件结构上,三者并没有什么不同,它们都采用zip或jar档案文件压缩格式,当时它们的使用有所区别:Jar(JavaApplicationArchive)文件包含Java类的普通库,资源辅助文件
Stella981 Stella981
2年前
CentOS 安装 rar、zip 解压缩
Windows系统压缩的rar和zip文件,在Linux系统下是无法通过tar命令解压缩的,需要使用rar和zip命令来解压缩。下面记录一下rar和zip安装和简单的使用。一、rar安装使用Linux系统下使用rarlinux解压缩rar压缩文件,下载页面:http://www.r
Wesley13 Wesley13
2年前
linux常见压缩格式及deb安装包用法
ZIP格式评价:可能是目前使用的最多的文档压缩格式,跨平台。压缩率不高。压缩一个目录:\ziprarchive\_name.zipdirectory\_to\_compress解压缩:\unziparchive\_name.zipTAR格式评价:消耗cpu及时间少,仅仅是一个打包工具,并不负责压缩。
Stella981 Stella981
2年前
Linux基础学习笔记——压缩和解压(tar、zip)
压缩和解压缩Linux默认支持的压缩格式:gz、bz2、zip压缩目的:节省磁盘空间扩展:1.压缩文件尽量使用gz,因为占用空间较少,效率高;2.bz2压缩后的文件占用空间最小;3.zip压缩的文件占用空
Wesley13 Wesley13
2年前
C#压缩解压缩文件(zip格式)
using System;using System.Collections.Generic;using System.IO;using ICSharpCode.SharpZipLib.Zip;namespace TestConsole{    internal class Prog
Wesley13 Wesley13
2年前
Linux 运维 9月30日 笔记 6.1
目录一、压缩打包介绍二、gzip压缩工具三、bzip2压缩工具四、xz压缩工具一、压缩打包介绍1.常见压缩文件Windows.rar.zip.7zLinux.zip.gz.bz2.xz.tar 二、gzip压缩工具
燕青 燕青
4个月前
苹果专用压缩解压工具:BetterZip 5中文注册激活版 附 激活码
是一款Mac操作系统上的压缩和解压缩工具,它可以帮助用户创建、打开、浏览和提取各种不同类型的压缩文件。使用BetterZip,您可以通过简单的拖放动作将文件添加到压缩包中,并选择不同的压缩格式(如zip、tar、7zip、rar等)。此外,BetterZi
子桓 子桓
6个月前
高质量压缩 Keka for Mac中文最新介绍
Keka是一款简单易用的Mac平台上的压缩解压缩工具。它支持多种压缩格式,包括常见的ZIP、RAR、7ZIP、TAR等,可以快速、高效地压缩和解压缩各种文件和文件夹。以下是关于Keka软件的一些详细介绍:支持多种压缩格式。Keka支持多种常用压缩格式,包括