The file I/O described earlier, whether it is written to text or objects, basically looks like the data in the file. You can browse the general outline with text editing software such as Notepad.It is convenient and convenient to store data like this, but it is not economical for two reasons: First, there may be a lot of duplicate information in the written data, but if it is written to the file as it is, undoubtedly a lot of redundant data will be retained, resulting in a waste of space; Second, the written data is saved in clear text, which is prone to information disclosure and low security.Java provides a simple compression and decompression tool that compresses the data before it is written to a file, and then writes the compressed result to a file. Similarly, when you read a compressed file, you read the compressed data first, then decompress it. The decompressed result is the original data.
In the family system of IO streams, compression and decompression operations require four tool classes, GZIPOutputStream, GZIPInputStream, ByteArrayOutputStream, and ByteArrayInputStream, to work together, which are briefly described below:
GZIPOutputStream: Compresses the output stream.It eats an array of bytes of the original data and pulls out a byte array output stream object (compressed data).
ByteArrayOutputStream: Byte array output stream.It takes compressed data from a compressed output stream and outputs byte array information through the toByteArray method.Or, the decompressed data can be obtained from the compressed input stream and the byte array information can be output by the toByteArray method.
GZIPInputStream: Compresses the input stream.It eats the byte array input stream object (compressed data) and pulls out the decompressed byte array (raw data).
ByteArrayInputStream: Byte array input stream.It enters an array of bytes of compressed data, converts it to a stream object, and discards it to the compressed input stream.
The description of the tools above seems uninteresting and needs to be used in real cases to make sense.Next, to see how the original string becomes compressed data, a detailed code example of the compression process is shown below:
// Get a compressed byte array from a string private static byte[] compress(String str) { if (str==null || str.length()<=0) { return null; } byte[] zip_bytes = null; // Byte array declaring compressed data // Build byte array output stream first, then compressed output stream from it try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gos = new GZIPOutputStream(baos);) { gos.write(str.getBytes()); // Write byte array to compressed output stream gos.finish(); // End Write Operation zip_bytes = baos.toByteArray(); // Getting byte array information from byte array output stream } catch (Exception e) { e.printStackTrace(); } return zip_bytes; }
Writing a compressed array of bytes to a file is a snap. Here's an example of the code for writing compressed data to a file:
// Write compressed data to a file private static void writeZipFile() { String str = "The sun beyond the mountains glows; the Yellow Riverseawards flows.\n You can enjoy a grander sight by climbing to a greater height."; // Construct a file output stream object based on the specified file path try (FileOutputStream fos = new FileOutputStream(mFileName)) { // Get a compressed byte array from a string byte[] zip_bytes = compress(str); fos.write(zip_bytes); // Write the byte array to the file output stream } catch (Exception e) { e.printStackTrace(); } }
Let's see how to read the decompressed raw data from a compressed file. Restoring the compressed data to an initial string is more complex. It requires three tools, ByteArrayInputStream, GZIPInputStream, and ByteArrayOutputStream, to work together. The decompression code is as follows:
// Obtaining decompressed strings from a compressed byte array private static String uncompress(byte[] bytes) { if (bytes==null || bytes.length<=0) { return null; } byte[] unzip_bytes = null; // Byte array declaring decompressed data // Build byte array output stream and byte array input stream, respectively, and build compressed input stream from byte array input stream try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); GZIPInputStream gis = new GZIPInputStream(bais);) { byte[] buffer = new byte[1024]; while (true) { // Reads data from a compressed input stream to a byte array and returns the length of the read data int length = gis.read(buffer); if (length < 0) { // No data read, indicating that you have finished reading break; } baos.write(buffer); // Write a byte array to the byte array output stream } unzip_bytes = baos.toByteArray(); // Getting byte array information from byte array output stream } catch (Exception e) { e.printStackTrace(); } return new String(unzip_bytes); // Converts an array of bytes to a string and returns the string }
With the uncompress decompression method just written, it is easy to get the original string from the compressed file. Here is an example of the code that reads the decompressed data from the compressed file:
// Read decompressed data from a compressed file private static void readZipFile() { // Construct file input stream objects based on specified file paths try (FileInputStream fis = new FileInputStream(mFileName)) { // Allocates an array of bytes of file size.The available method returns the current unread size byte[] bytes = new byte[fis.available()]; fis.read(bytes); // Read byte array from file input stream // Obtaining decompressed strings from a compressed byte array String content = uncompress(bytes); System.out.println("content="+content); } catch (Exception e) { e.printStackTrace(); } }
For more Java technical articles see " Java Development Notes (Sequence) Chapter Program Directory>