温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用brotli压缩文件和解压缩

发布时间:2020-05-29 10:05:28 来源:亿速云 阅读:4717 作者:Leah 栏目:云计算

这篇文章主要为大家详细介绍了使用brotli压缩文件和解压缩的方法,文章还展示了示例代码,适合刚入门的初学者,感兴趣的小伙伴们可以参考一下。

制作压缩文件

下面我先介绍一下如何制作压缩文件。下面的代码和用例都来自于项目 packed-selenium-java-example 。

安装 brotli 命令

Mac 用户

brew install brotli

Windows 用户可以去这个界面下载,https://github.com/google/brotli/releases

打包并压缩

打包前两个文件大小分别为 7.5M 和 97M

╭─ ~/D/test1[◷ 18:15:21]
╰─  ll
total 213840
-rwxr-xr-x  1 vangie  staff   7.5M  3  5 11:13 chromedriver
-rwxr-xr-x  1 vangie  staff    97M  1 25  2018 headless-chromium

使用 GZip 打包并压缩,大小为 44 M。

╭─ ~/D/test1[◷ 18:15:33]
╰─  tar -czvf chromedriver.tar chromedriver headless-chromium
a chromedriver
a headless-chromium
╭─ ~/D/test1[◷ 18:16:41]
╰─  ll
total 306216
-rwxr-xr-x  1 vangie  staff   7.5M  3  5 11:13 chromedriver
-rw-r--r--  1 vangie  staff    44M  3  6 18:16 chromedriver.tar
-rwxr-xr-x  1 vangie  staff    97M  1 25  2018 headless-chromium

tar 去掉 z 选项再打包一遍,大小为 104M

╭─ ~/D/test1[◷ 18:16:42]
╰─  tar -cvf chromedriver.tar chromedriver headless-chromium
a chromedriver
a headless-chromium
╭─ ~/D/test1[◷ 18:17:06]
╰─  ll
total 443232
-rwxr-xr-x  1 vangie  staff   7.5M  3  5 11:13 chromedriver
-rw-r--r--  1 vangie  staff   104M  3  6 18:17 chromedriver.tar
-rwxr-xr-x  1 vangie  staff    97M  1 25  2018 headless-chromium

压缩后的大小为 33M,相比 Gzip 的 44M 小了不少。耗时也非常的感人 6 分 18 秒,Gzip 只要 5 秒。

╭─ ~/D/test1[◷ 18:17:08]
╰─  time brotli -q 11 -j -f chromedriver.tar
brotli -q 11 -j -f chromedriver.tar  375.39s user 1.66s system 99% cpu 6:18.21 total
╭─ ~/D/test1[◷ 18:24:23]
╰─  ll
total 281552
-rwxr-xr-x  1 vangie  staff   7.5M  3  5 11:13 chromedriver
-rw-r--r--  1 vangie  staff    33M  3  6 18:17 chromedriver.tar.br
-rwxr-xr-x  1 vangie  staff    97M  1 25  2018 headless-chromium

运行时解压缩

下面以 java maven 项目为例

添加解压依赖包

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.18</version>
</dependency>

<dependency>
    <groupId>org.brotli</groupId>
    <artifactId>dec</artifactId>
    <version>0.1.2</version>
</dependency>

commons-compress 是 apache 提供的解压缩工具包,对于各种压缩算法提供一致的抽象接口,其中对于 brotli 算法只支持解压,这里足够了。org.brotli:dec 包是 Google 提供的 brotli 解压算法的底层实现。

实现 initialize 方法

public class ChromeDemo implements  FunctionInitializer {

    public void initialize(Context context) throws IOException {

        Instant start = Instant.now();

        try (TarArchiveInputStream in =
                     new TarArchiveInputStream(
                             new BrotliCompressorInputStream(
                                     new BufferedInputStream(
                                             new FileInputStream("chromedriver.tar.br"))))) {

            TarArchiveEntry entry;
            while ((entry = in.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    continue;
                }
                File file = new File("/tmp/bin", entry.getName());
                File parent = file.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                }

                System.out.println("extract file to " + file.getAbsolutePath());

                try (FileOutputStream out = new FileOutputStream(file)) {
                    IOUtils.copy(in, out);
                }

                Files.setPosixFilePermissions(file.getCanonicalFile().toPath(),
                        getPosixFilePermission(entry.getMode()));
            }
        }

        Instant finish = Instant.now();
        long timeElapsed = Duration.between(start, finish).toMillis();

        System.out.println("Extract binary elapsed: " + timeElapsed + "ms");

    }
}

实现 FunctionInitializer 接口的 initialize 方法。解压过程刚开始是四层嵌套流,作用分别如下:

  1. FileInputStream 读取文件
  2. BufferedInputStream 提供缓存,介绍系统调用带来的上下文切换,提示读取的速度
  3. BrotliCompressorInputStream 对字节流进行解码
  4. TarArchiveInputStream 把 tar 包里的文件逐个解出来

然后 Files.setPosixFilePermissions 的作用是还原 tar 包中文件的权限。代码太长此处略去,参阅 packed-selenium-java-example

Instant start = Instant.now();
...

Instant finish = Instant.now();
long timeElapsed = Duration.between(start, finish).toMillis();

System.out.println("Extract binary elapsed: " + timeElapsed + "ms");

上面的代码段会打印出解压的耗时,真实执行大概在 3.7 s 左右。

最后不要忘记在 template.yml 里配置上 Initializer 和 

InitializationTimeout

猜你想要:

1.智能压缩使用 Gzip 还是 Brotli

2.Nginx启用Brotli算法压缩示例

以上就是brotli压缩文件和解压缩的使用方法,详细使用情况还得要大家自己使用过才能知道具体要领。如果想阅读更多相关内容的文章,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI