生活随笔
收集整理的這篇文章主要介紹了
数据的gzip压缩解压缩_使用GZIP和压缩数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據的gzip壓縮解壓縮
抽象
我們都知道用zip或gzip壓縮文件的含義。 但是在Java中使用壓縮文件并不像您想的那樣簡單,特別是如果您不是直接使用文件而是壓縮流數據時。 我們會去:
- 如何將字符串轉換為壓縮/壓縮字節數組,反之亦然
- 創建用于讀取和寫入文件的實用程序功能,而無需事先知道文件或流是否已gzip壓縮。
基礎
那么,為什么要壓縮任何東西? 很簡單,因為這是減少必須通過網絡傳送或存儲到磁盤的數據量的好方法,因此可以提高操作速度。 根據文檔的性質,典型的文本文件或消息可以減少10倍或更多。 當然,您將不得不考慮壓縮和解壓縮的成本,但是當您擁有大量數據時,這些成本將不會很大。
Java支持嗎?
是的,Java支持在java.util.zip包中讀寫gzip文件。 它還支持zip文件以及流行的ZLIB壓縮庫的數據膨脹和縮小。
如何壓縮/解壓縮Java字符串?
這是有關如何使用DeflaterOutputStream壓縮和解壓縮String的示例。
這是使用Java內置壓縮器的兩種方法以及使用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();}} 如何解碼字節流以同時允許GZip和普通流:
下面的代碼將字節流轉換為String(轉儲),而無需事先知道該流是否已壓縮。
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方法的實現。 揭示關于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
數據的gzip壓縮解壓縮
總結
以上是生活随笔為你收集整理的数据的gzip压缩解压缩_使用GZIP和压缩数据的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。