日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据的gzip压缩解压缩_使用GZIP和压缩数据

發(fā)布時間:2023/12/3 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据的gzip压缩解压缩_使用GZIP和压缩数据 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

數(shù)據(jù)的gzip壓縮解壓縮

抽象

我們都知道用zip或gzip壓縮文件的含義。 但是在Java中使用壓縮文件并不像您想的那樣簡單,特別是如果您不是直接使用文件而是壓縮流數(shù)據(jù)時。 我們會去:

  • 如何將字符串轉(zhuǎn)換為壓縮/壓縮字節(jié)數(shù)組,反之亦然
  • 創(chuàng)建用于讀取和寫入文件的實用程序功能,而無需事先知道文件或流是否已gzip壓縮。

基礎(chǔ)

那么,為什么要壓縮任何東西? 很簡單,因為這是減少必須通過網(wǎng)絡(luò)傳送或存儲到磁盤的數(shù)據(jù)量的好方法,因此可以提高操作速度。 根據(jù)文檔的性質(zhì),典型的文本文件或消息可以減少10倍或更多。 當(dāng)然,您將不得不考慮壓縮和解壓縮的成本,但是當(dāng)您擁有大量數(shù)據(jù)時,這些成本將不會很大。

Java支持嗎?

是的,Java支持在java.util.zip包中讀寫gzip文件。 它還支持zip文件以及流行的ZLIB壓縮庫的數(shù)據(jù)膨脹和縮小。

如何壓縮/解壓縮Java字符串?

這是有關(guān)如何使用DeflaterOutputStream壓縮和解壓縮String的示例。

這是使用Java內(nèi)置壓縮器的兩種方法以及使用GZIP的方法:

  • 使用DeflaterOutputStream是最簡單的方法: enum StringCompressor {;public static byte[] compress(String text) {ByteArrayOutputStream baos = new ByteArrayOutputStream();try {OutputStream out = new DeflaterOutputStream(baos);out.write(text.getBytes("UTF-8"));out.close();} catch (IOException e) {throw new AssertionError(e);}return baos.toByteArray();}public static String decompress(byte[] bytes) {InputStream in = new InflaterInputStream(new ByteArrayInputStream(bytes));ByteArrayOutputStream baos = new ByteArrayOutputStream();try {byte[] buffer = new byte[8192];int len;while((len = in.read(buffer))>0)baos.write(buffer, 0, len);return new String(baos.toByteArray(), "UTF-8");} catch (IOException e) {throw new AssertionError(e);}}}
  • 如果要直接使用充氣機/充氣機: enum StringCompressor2 {;public static byte[] compress(String text) throws Exception{byte[] output = new byte;Deflater compresser = new Deflater();compresser.setInput(text.getBytes("UTF-8"));compresser.finish();int compressedDataLength = compresser.deflate(output);byte[] dest = new byte[compressedDataLength];System.arraycopy(output, 0, dest, 0, compressedDataLength);return dest;}public static String decompress(byte[] bytes) throws Exception{Inflater decompresser = new Inflater();decompresser.setInput(bytes, 0, bytes.length);byte[] result = new byte[bytes.length *10];int resultLength = decompresser.inflate(result);decompresser.end();// Decode the bytes into a StringString outputString = new String(result, 0, resultLength, "UTF-8");return outputString;}}
  • 使用GZIP的方法如下: enum StringGZipper {;private static String ungzip(byte[] bytes) throws Exception{InputStreamReader isr = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(bytes)), StandardCharsets.UTF_8);StringWriter sw = new StringWriter();char[] chars = new char[1024];for (int len; (len = isr.read(chars)) > 0; ) {sw.write(chars, 0, len);}return sw.toString();}private static byte[] gzip(String s) throws Exception{ByteArrayOutputStream bos = new ByteArrayOutputStream();GZIPOutputStream gzip = new GZIPOutputStream(bos);OutputStreamWriter osw = new OutputStreamWriter(gzip, StandardCharsets.UTF_8);osw.write(s);osw.close();return bos.toByteArray();}}
  • 如何解碼字節(jié)流以同時允許GZip和普通流:

    下面的代碼將字節(jié)流轉(zhuǎn)換為String(轉(zhuǎn)儲),而無需事先知道該流是否已壓縮。

    if (isGZIPStream(bytes)) {InputStreamReader isr = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(bytes)), StandardCharsets.UTF_8);StringWriter sw = new StringWriter();char[] chars = new char[1024];for (int len; (len = isr.read(chars)) > 0; ) {sw.write(chars, 0, len);}dump = sw.toString();} else {dump = new String(bytes, 0, length, StandardCharsets.UTF_8);} }

    這是isGZIPStream方法的實現(xiàn)。 揭示關(guān)于GZIP_MAGIC背后的真相!

    public static boolean isGZIPStream(byte[] bytes) {return bytes[0] == (byte) GZIPInputStream.GZIP_MAGIC && bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >>> 8); }

    這是一種在不知道文件是否已壓縮的情況下讀取文件的簡單方法(依賴于擴展名.gz)。

    static Stream<String> getStream(String dir, @NotNull String fileName) throws IOException {File file = new File(dir, fileName);InputStream in;if (file.exists()) {in = new FileInputStream(file);} else {file = new File(dir, fileName + ".gz");in = new GZIPInputStream(new FileInputStream(file));}return new BufferedReader(new InputStreamReader(in)).lines(); }

    翻譯自: https://www.javacodegeeks.com/2015/01/working-with-gzip-and-compressed-data.html

    數(shù)據(jù)的gzip壓縮解壓縮

    總結(jié)

    以上是生活随笔為你收集整理的数据的gzip压缩解压缩_使用GZIP和压缩数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。