资讯专栏INFORMATION COLUMN

java实现zip,gzip,7z,zlib格式的压缩打包

Mertens / 551人阅读

摘要:前言利用原生类和的实现的压缩打包依赖格式读入需要下载的文件的内容,打包到文件打包设置压缩文件里的文件名打包打包

前言

利用Java原生类和apache的commons实现zip,gzip,7z,zlib的压缩打包

maven依赖

 
            org.apache.commons
            commons-compress
            1.12
          

zip格式

public static void zip(String input, String output, String name) throws Exception {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(output));
        String[] paths = input.split("|");
        File[] files = new File[paths.length];
        byte[] buffer = new byte[1024];
        for (int i = 0; i < paths.length; i++) {
            files[i] = new File(paths[i]);
        }
        for (int i = 0; i < files.length; i++) {
            FileInputStream fis = new FileInputStream(files[i]);
            if (files.length == 1 && name != null) {
                out.putNextEntry(new ZipEntry(name));
            } else {
                out.putNextEntry(new ZipEntry(files[i].getName()));
            }
            int len;
            // 读入需要下载的文件的内容,打包到zip文件
            while ((len = fis.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.closeEntry();
            fis.close();
        }
        out.close();
    }

gzip打包

public static void gzip(String input, String output, String name) throws Exception {
        String compress_name = null;
        if (name != null) {
            compress_name = name;
        } else {
            compress_name = new File(input).getName();
        }
        byte[] buffer = new byte[1024];
        try {
            GzipParameters gp = new GzipParameters();    //设置压缩文件里的文件名
            gp.setFilename(compress_name);
            GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(new FileOutputStream(output), gp);
            FileInputStream fis = new FileInputStream(input);
            int length;
            while ((length = fis.read(buffer)) > 0) {
                gcos.write(buffer, 0, length);
            }
            fis.close();
            gcos.finish();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

7z打包

public static void z7z(String input, String output, String name) throws Exception {
        try {
            SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File(output));
            SevenZArchiveEntry entry = null;

            String[] paths = input.split("|");
            File[] files = new File[paths.length];
            for (int i = 0; i < paths.length; i++) {
                files[i] = new File(paths[i].trim());
            }
            for (int i = 0; i < files.length; i++) {
                BufferedInputStream instream = null;
                instream = new BufferedInputStream(new FileInputStream(paths[i]));
                if (name != null) {
                    entry = sevenZOutput.createArchiveEntry(new File(paths[i]), name);
                } else {
                    entry = sevenZOutput.createArchiveEntry(new File(paths[i]), new File(paths[i]).getName());
                }
                sevenZOutput.putArchiveEntry(entry);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = instream.read(buffer)) > 0) {
                    sevenZOutput.write(buffer, 0, len);
                }
                instream.close();
                sevenZOutput.closeArchiveEntry();
            }
            sevenZOutput.close();
        } catch (IOException ioe) {
            System.out.println(ioe.toString() + "  " + input);
        }
    }

zlib打包

public static void zlib(String input, String output) throws Exception {

//        DeflaterOutputStream dos = new DeflaterOutputStream(new FileOutputStream(output));
        DeflateParameters dp = new DeflateParameters();
        dp.setWithZlibHeader(true);
        DeflateCompressorOutputStream dcos = new  DeflateCompressorOutputStream(new FileOutputStream(output),dp);
        FileInputStream fis = new FileInputStream(input);
        int length = (int) new File(input).length();
        byte data[] = new byte[length];
        // int length;
        while ((length = fis.read(data)) > 0) {
            dcos.write(data, 0, length);
        }
        fis.close();
        dcos.finish();
        dcos.close();
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65086.html

相关文章

  • HTTP协议中你必须知道三种数据格式

    摘要:分块传输编码分块传输编码是超文本传输协议中的一种数据传输机制,允许由网页服务器发送给客户端应用通常是网页浏览器的数据可以分成多个部分。分块传输编码只在协议版本中提供。 实习中的一个主要工作就是分析 HTTP 中的协议,自己也用 Python 写过正则表达式对 HTTP 请求和响应的内容进行匹配,然后把关键字段抽离出来放到一个字典中以备使用(可以稍微改造一下就是一个爬虫工具)。 HTTP...

    Blackjun 评论0 收藏0
  • Swoole WebSoctet 使用 zlib 压缩之 PHP 与 pako.js

    摘要:一些理论知识先说一下算法吧,是压缩文件的默认算法,其实现在不光用在文件中在等其他的压缩文件中都用,实际上只是一种压缩数据流的算法,任何需要流式压缩的地方都可以用。也就是说格式格式,是文件格式,是这些文件格式使用的压缩算法。 一些理论知识 先说一下deflate算法吧,deflate是zip压缩文件的默认算法, 其实deflate现在不光用在zip文件中, 在7z, xz等其他的压缩文件...

    ASCH 评论0 收藏0
  • 微信Android资源混淆打包工具,如何让应用安装包立减1M

    摘要:腾讯特约作者微信客户端高级工程师微信中的资源混淆工具主要为了混淆资源长度例如将混淆为,同时利用深度压缩,大大减少了安装包体积,同时也增加了逼格,提升了反破解难度。写在前言资源混淆工具大约是在年月实现,并在微信中使用,减少了大约的空间。 腾讯Bugly特约作者: 微信客户端高级工程师 shwen 微信中的资源混淆工具主要为了混淆资源ID长度(例如将res/drawable/welcome...

    Honwhy 评论0 收藏0

发表评论

0条评论

Mertens

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<