Skip to content

Instantly share code, notes, and snippets.

@oakhole
Last active January 1, 2016 19:39
Show Gist options
  • Save oakhole/8191653 to your computer and use it in GitHub Desktop.
Save oakhole/8191653 to your computer and use it in GitHub Desktop.
gzip 打包/解包 文件
import java.util.zip.GZIPOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class CompressFileGZIP {
private static void doCompressFile(String inFileName) {
try {
System.out.println("Creating the GZIP output stream.");
String outFileName = inFileName + ".gz";
GZIPOutputStream out = null;
try {
out = new GZIPOutputStream(new FileOutputStream(outFileName));
} catch(FileNotFoundException e) {
System.err.println("Could not create file: " + outFileName);
System.exit(1);
}
System.out.println("Opening the input file.");
FileInputStream in = null;
try {
in = new FileInputStream(inFileName);
} catch (FileNotFoundException e) {
System.err.println("File not found. " + inFileName);
System.exit(1);
}
System.out.println("Transfering bytes from input file to GZIP Format.");
byte[] buf = new byte[1024];
int len;
while((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
System.out.println("Completing the GZIP file");
out.finish();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java CompressFileGZIP filename");
} else {
doCompressFile(args[0]);
}
}
}
import java.util.zip.GZIPInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class UncompressFileGZIP {
public static void doUncompressFile(String inFileName) {
try {
if (!getExtension(inFileName).equalsIgnoreCase("gz")) {
System.err.println("File name must have extension of \".gz\"");
System.exit(1);
}
System.out.println("Opening the compressed file.");
GZIPInputStream in = null;
try {
in = new GZIPInputStream(new FileInputStream(inFileName));
} catch(FileNotFoundException e) {
System.err.println("File not found. " + inFileName);
System.exit(1);
}
System.out.println("Open the output file.");
String outFileName = getFileName(inFileName);
FileOutputStream out = null;
try {
out = new FileOutputStream(outFileName);
} catch (FileNotFoundException e) {
System.err.println("Could not write to file. " + outFileName);
System.exit(1);
}
System.out.println("Transfering bytes from compressed file to the output file.");
byte[] buf = new byte[1024];
int len;
while((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
System.out.println("Closing the file and stream");
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public static String getExtension(String f) {
String ext = "";
int i = f.lastIndexOf('.');
if (i > 0 && i < f.length() - 1) {
ext = f.substring(i+1);
}
return ext;
}
public static String getFileName(String f) {
String fname = "";
int i = f.lastIndexOf('.');
if (i > 0 && i < f.length() - 1) {
fname = f.substring(0,i);
}
return fname;
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java UncompressFileGZIP gzipfile");
} else {
doUncompressFile(args[0]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment