java - Where to start in building an app/system that compresses and decompresses a file -
i'm developing application in java/android allows user compress , decompress files. @ first, started study file size such as:
1byte = 8bits 1kb = 1024byte 1mb = 1024kb 1gb = 1024mb 1tb = 1024gb 1pb = 1024tb 1eb = 1024pb 1zb = 1024eb 1yb = 1024zb after studied this, studied , read articles on net , found out there 2 types of file compression (correct me if i'm wrong): lossless , lossy. lossless compression means file compressed smaller bit without losing single file while lossy compression means important files being removed while compressing file.
i read compression(run-length coding method) this:
aaabbccdffffeeeeh to this:
3a2b2cd4f4eh which gives me idea on how compressing/decompressing works on file.
i searched net there api compressing file on java(also applicable on android) is
java.util.zip i tried codes on compressing , decompressing file various helpful websites/forum/etc (including stackoverflow.com) gives me experience study.
i read algorithms used in data compression are
huffman encoding algorithm - assigns code characters in file based on how characters occur run-length encoding - generates two-part value repeated characters: first part specifies number of times character repeated, , second part identifies character lempel-ziv algorithm - converts variable-length strings fixed-length codes consume less space original strings. now, need know how code algo in compressing , decompressing file using java.util.zip(i don't know how use it. tutorials on net not working me :/). algo winzip, winrar, compressed folder(windows), , androzip(android app) using? please teach me step step(treat me unschooled person) on how java.util.zip works , different algorithms. sorry long post folks. future , posts(if there be)!
public static final byte[] unzip(byte[] in) throws ioexception { // decompress using gzipinputstream bytearrayoutputstream outstream = new bytearrayoutputstream(expected_compression_ratio * in.length); gzipinputstream instream = new gzipinputstream ( new bytearrayinputstream(in) ); byte[] buf = new byte[buf_size]; while (true) { int size = instream.read(buf); if (size <= 0) break; outstream.write(buf, 0, size); } outstream.close(); return outstream.tobytearray(); } public static final byte[] zip(byte[] in) { try { // compress using gzipoutputstream bytearrayoutputstream byteout= new bytearrayoutputstream(in.length / expected_compression_ratio); gzipoutputstream outstream= new gzipoutputstream(byteout); try { outstream.write(in); } catch (exception e) { log.error("", e); } try { outstream.close(); } catch (ioexception e) { log.error("", e); } return byteout.tobytearray(); } catch (ioexception e) { log.error("", e); return null; } }
Comments
Post a Comment