Revision as of 17:02, 22 March 2011 by Han84 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
public static void fileCompressEn(String fileNameNormal, String fileNameCompressed) {
	java.io.BufferedInputStream in = Ts.convertFileToBufferedInputStream(fileNameNormal);
	try {
		java.io.BufferedOutputStream out = new java.io.BufferedOutputStream(new java.util.zip.GZIPOutputStream(new java.io.FileOutputStream(fileNameCompressed)));
		byte[] buff = new byte[1024 * 4];
		int len;
		while ((len = in.read(buff)) >= 0) {
			out.write(buff, 0, len);
		}
		in.close();
		out.close();
	} catch (java.io.IOException ex) {
		Ts.printErr(ex);
	}
}
 
public static void fileCompressDe(String fileNameCompressed, String fileNameNormal) {
	java.io.BufferedInputStream in = null;
	java.io.BufferedOutputStream out = Ts.convertFileToBufferedOutputStream(fileNameNormal);
	try {
		in = new java.io.BufferedInputStream(new java.util.zip.GZIPInputStream(new java.io.FileInputStream(fileNameCompressed)));
	} catch (java.io.FileNotFoundException ex) {
		Ts.printErr(ex);
	} catch (java.io.IOException ex) {
		Ts.printErr(ex);
	}
	try {
		byte[] buff = new byte[1024 * 4];
		int len;
		while ((len = in.read(buff)) >= 0) {
			out.write(buff, 0, len);
		}
		in.close();
		out.close();
	} catch (java.io.IOException ex) {
		Ts.printErr(ex);
	}
}
 
private static void fileCompressTest() {
	String normalFileName = "c:/result002.txt";
	String normalFileName2 = "c:/result002_1.txt";
	String compressedFileName = "c:/result002.txt.gz";
	Ts.fileCompressEn(normalFileName, compressedFileName);
	Ts.fileCompressDe(compressedFileName, normalFileName2);
	System.out.println("identical? " + Ts.fileIsIdentical(normalFileName, normalFileName2));
}
 
public static java.io.BufferedInputStream convertFileToBufferedInputStream(String fileName) {
	return Ts.convertFileToBufferedInputStream(new java.io.File(fileName));
}
 
public static java.io.BufferedInputStream convertFileToBufferedInputStream(java.io.File file) {
	java.io.BufferedInputStream bis = null;
	try {
		bis = new java.io.BufferedInputStream(new java.io.FileInputStream(file));
	} catch (java.io.FileNotFoundException ex) {
		Ts.printErr(ex);
	}
	return bis;
}
 
public static void printErr(java.lang.Exception ex) {
	ex.printStackTrace();
	System.exit(-1);
}
 
public static java.io.BufferedOutputStream convertFileToBufferedOutputStream(String fileName) {
	return Ts.convertFileToBufferedOutputStream(new java.io.File(fileName));
}
 
public static java.io.BufferedOutputStream convertFileToBufferedOutputStream(java.io.File file) {
	java.io.BufferedOutputStream bis = null;
	try {
		bis = new java.io.BufferedOutputStream(new java.io.FileOutputStream(file));
	} catch (java.io.FileNotFoundException ex) {
		Ts.printErr(ex);
	}
	return bis;
}


Back to JavaHowTo

Alumni Liaison

To all math majors: "Mathematics is a wonderfully rich subject."

Dr. Paul Garrett