// Jay Summet, Georgia Tech // CS 1316, Summer 2009 // //Demonstrates writing to files using the GZIP stream and then // reading GZIPed data back. It also illustrates using object // "chaining" where multiple stream handling objects pass data // on to the next object to transform it multiple ways. import java.io.*; import java.util.zip.*; public class GZIPdemo { private static final String ourString = "The Romane without doubte, and I will tell the commoditie, and" + "the discommoditie of the one, and the other. The Dutche footemen, are"+ "able to withstande, and overcome the horses: they bee moste speedie to"+ "marche, and to be set in araye, being not laden with armours: of the"+ "other part, they be subjecte to all blowes, both farre of, and at hande:"+ "because they be unarmed, they bee unprofitable unto the battaile on the"+ "lande, and to everye fighte, where is strong resistaunce. But the"+ "Romanes withstoode, and overcame the horses, as well as the Dutchemen,"+ "they were safe from blowes at hande, and farre of, being covered with"+ "armours: they were also better able to charge, and better able to"+ "sustaine charges, having Targaettes: they might more aptly in the preace"+ "fight with the swoorde, then these with the Pike, and though the"+ "Dutchemen have likewise swoordes, yet being without Targaets, they"+ "become in suche case unprofitable: The Romanes might safelye assaul't"+ "townes, having their bodies cleane covered with armour, and being better"+ "able to cover themselves with their Targaettes. So that they had no"+ "other incommoditie, then the waightynesse of their armours, and the pain"+ "to cary them: the whiche thinges thei overcame, with accustomyng the"+ "body to diseases, and with hardenyng it, to bee able to indure labour."+ "And you knowe, how that in thinges accustomed, men suffer no grief. And"+ "you have to understand this, that the footemen maie be constrained, to"+ "faight with footemen, and with horse, and alwaies those be unprofitable,"+ "whiche cannot either sustain the horses, or beyng able to sustain them,"+ "have notwithstandyng neede to feare the footemen, whiche be better"+ "armed, and better ordeined then thei. Now if you consider the Duchemen,"+ "and the Romaines, you shall finde in the Duchemen activitie (as we have"+ "said) to overcome the horses, but greate dissavauntage, when thei"+ "faighte with menne, ordeined as thei them selves are, and armed as the"+ "Romaines were: so that there shall be this advauntage more of the one,"+ "then of thother, that the Romaines could overcome the men, and the"+ "horses, the Duchemen onely the horses."; // Writes a string (Characters) to an outputStreamWriter, which converts // the characters to bytes that are written out the FileOutputStream. public void write(String filename) { try { // The file object. File f = new File(filename); // File output streams write to a file.... FileOutputStream fos = new FileOutputStream(f); //OutputStreamWriter converts charactrs in strings to bytes. OutputStreamWriter osw = new OutputStreamWriter(fos); // write out a bit of text osw.write( ourString); osw.close(); } catch (Exception e) { System.out.println("Exception happened! " + e.getMessage()); } } // Writes a string (Characters) to an outputStreamWriter, which converts // the characters to bytes that are written out to the GZIPOutputStream // the compresses the string before sending it to the FileOutputStream. public void GZIPwrite( String filename) { try { // Make a OutputWriterStream -> GZIPOutputStream -> FileOutputStream -> File stream... File f = new File(filename); // Writes the bytes to the file. FileOutputStream fos = new FileOutputStream( f) ; // GZIP's the bytes as they come out of the Output Stream Writer GZIPOutputStream gos = new GZIPOutputStream( fos ); // Converts characters to bytes OutputStreamWriter osw = new OutputStreamWriter( gos); // Write a bit of text... osw.write( ourString); osw.close(); } catch (Exception e) { System.out.println("Exception happened! " + e.getMessage()); } } // end write() public void GZIPread( String filename) { try { // Make File -> FileInputStream -> GZIPInputStream -> InputStreamReader -> BufferedReader File f = new File(filename); // Reads (compressed) bytes from the file. FileInputStream fis = new FileInputStream( f) ; // Uncompresses the compressed bytes GZIPInputStream gis = new GZIPInputStream( fis ); // Converts the bytes to characters. InputStreamReader isr = new InputStreamReader( gis); // Buffers up a line at a time, allowing you to read a full line at once. BufferedReader reader = new BufferedReader(isr); String line = null; // Read in the text! while( (line = reader.readLine()) != null) { System.out.println(line + "\n"); } reader.close(); } catch (Exception e) { System.out.println("Exception happened! " + e.getMessage()); } } // end read() public static void main( String[] args) { GZIPdemo gzd = new GZIPdemo(); gzd.write("output.txt"); gzd.GZIPwrite( "output.gzip"); // Just to prove we can read in the compressed text file: System.out.println("compressed file contents:\n"); gzd.GZIPread( "output.gzip"); } }