Help! We're trapped in the computer, and the computer is trapped in 2008! Someone call the time police!
0 Members and 1 Guest are viewing this topic.
package util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream;public class Compressor{ public static byte []compress(byte []data) { try { File tempFile = File.createTempFile("RCCompression", null); tempFile.deleteOnExit(); FileOutputStream fileOut = new FileOutputStream(tempFile); GZIPOutputStream out = new GZIPOutputStream(fileOut); out.write(data); out.flush(); out.finish(); fileOut.close(); FileInputStream in = new FileInputStream(tempFile); byte []bytes = new byte[in.available()]; in.read(bytes); tempFile.delete(); return bytes; } catch(IOException e) { System.err.println("Error compressing data: " + e); System.exit(1); return null; } } public static byte []decompress(byte []data) { try { Buffer b = new Buffer(); File tempFile = File.createTempFile("RCCompression", null); tempFile.deleteOnExit(); FileOutputStream fileOut = new FileOutputStream(tempFile); fileOut.write(data); fileOut.close(); FileInputStream fileIn = new FileInputStream(tempFile); GZIPInputStream in = new GZIPInputStream(fileIn); int i; while((i = in.read()) >= 0) b.addByte((byte) i); tempFile.delete(); return b.getBytes(); } catch(IOException e) { System.err.println("Error decompressing data: " + e); return new byte[0]; } } public static void main(String []args) { byte []data = "aaaaaaaaaaaaaa1 plumbing".getBytes(); byte []data2 = "A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... ".getBytes(); byte []compressedData = Compressor.compress(data); byte []compressedData2 = Compressor.compress(data2); System.out.format("Old length = %d\n", data.length); System.out.format("New length = %d\n", compressedData.length); System.out.format("Decompressed = %d\n", Compressor.decompress(compressedData).length); System.out.println(); System.out.format("Old length = %d\n", data2.length); System.out.format("New length = %d\n", compressedData2.length); System.out.format("Decompressed = %d\n", Compressor.decompress(compressedData2).length); System.out.println(); System.out.println(); System.out.format("Decompressed data: %s\n", new String(Compressor.decompress(compressedData))); System.out.format("Decompressed data: %s\n", new String(Compressor.decompress(compressedData2))); }}
Old length = 24New length = 33Decompressed = 24Old length = 551New length = 53Decompressed = 551Decompressed data: aaaaaaaaaaaaaa1 plumbingDecompressed data: A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data..... A lot of repeated data.....
I have a programming folder, and I have nothing of value there
Our species really annoys me.
I saw something about zlib, but then I worried that it had external dependencies so I didn't go into it. How does zlib ensure that the compressed data isn't larger?