Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use brotli compressed files to decompress

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces in detail the use of brotli compression files and decompression methods, the article also shows the sample code, suitable for beginners, interested friends can refer to it.

Creating compressed files

Here is how to create compressed files. The following code and use cases are from the packed-selenium-java-example project.

Install brotli command

Mac users

brew install brotli

Windows users can go to this interface to download, github.com/google/brotli/releases

Packing and compression

The first two file sizes are 7.5M and 97M.

╭─ ~/D/test1[◷ 18:15:21]╰─ lltotal 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

Packaged and compressed using GZip, the size is 44 MB.

╭─ ~/D/test1[◷ 18:15:33]╰─ tar -czvf chromedriver.tar chromedriver headless-chromiuma chromedrivera headless-chromium╭─ ~/D/test1[◷ 18:16:41]╰─ lltotal 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 Remove the z option and pack it again, the size is 104M

╭─ ~/D/test1[◷ 18:16:42]╰─ tar -cvf chromedriver.tar chromedriver headless-chromiuma chromedrivera headless-chromium╭─ ~/D/test1[◷ 18:17:06]╰─ lltotal 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

The compressed size is 33M, much smaller than Gzip's 44M. It also takes 6 minutes and 18 seconds, and Gzip takes only 5 seconds.

╭─ ~/D/test1[◷ 18:17:08]╰─ time brotli -q 11 -j -f chromedriver.tarbrotli -q 11 -j -f chromedriver.tar 375.39s user 1.66s system 99% cpu 6:18.21 total╭─ ~/D/test1[◷ 18:24:23]╰─ lltotal 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 runtime decompression

Take the java maven project as an example

Add decompression dependency package org.apache.commons commons-compress 1.18 org.brotli dec 0.1.2

Commons-compress is a decompression toolkit provided by apache, providing a consistent abstract interface for various compression algorithms, of which only decompression is supported for brotli algorithm, which is sufficient here. The org.brotli:dec package is the underlying implementation of the brotli decompression algorithm provided by Google.

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"); }}

Initialize method that implements FunctionInitializer interface. The decompression process starts with four nested flows, which function as follows:

FileInputStream reads files BufferedInputStream provides cache, introduces context switching brought by system calls, prompts read speed BrotliCompressorInputStream decodes byte stream TarArchiveInputStream unpacks files in tar package one by one

Files. setPosixFilePermission then acts to restore permissions to files in tarballs. Code too long omitted here, see 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");

The above code snippet will print out the decompression time, and the actual execution is about 3.7 s.

Finally, don't forget to configure Initializer and Initializer in template.yml

InitializationTimeout

Guess what you want:

1. Smart compression using Gzip or Brotli

2. Nginx enables Brotli algorithm compression example

The above is how to use brotli compression files and decompression, and the detailed use of the situation has to be used by everyone to know the specific essentials. If you want to read more articles related to content, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report