Java Tutorial/File/GZIPInputStream — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:19, 1 июня 2010
Содержание
Create GZIPInputStream from FileInputStream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
if (args[i].toLowerCase().endsWith(".gz")) {
try {
FileInputStream fin = new FileInputStream(args[i]);
GZIPInputStream gzin = new GZIPInputStream(fin);
FileOutputStream fout = new FileOutputStream(args[i].substring(0, args[i].length() - 3));
for (int c = gzin.read(); c != -1; c = gzin.read()) {
fout.write(c);
}
fout.close();
} catch (IOException ex) {
System.err.println(ex);
}
} else {
System.err.println(args[i] + " does not appear to be a gzipped file.");
}
}
}
}
Get Entry from a zip file
import java.io.FileInputStream;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ZipReader {
public static void main(String[] args) throws Exception {
ZipInputStream zis = null;
FileInputStream fis = new FileInputStream(args[0]);
zis = new ZipInputStream(fis);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null)
System.out.println(ze.getName() + " [" + ze.getSize() + "] ["
+ new Date(ze.getTime()).toString() + "]");
}
}
Ugzip using GZIPInputStream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
if (args[i].toLowerCase().endsWith(".gz")) {
try {
FileInputStream fin = new FileInputStream(args[i]);
GZIPInputStream gzin = new GZIPInputStream(fin);
FileOutputStream fout = new FileOutputStream(args[i].substring(0, args[i].length() - 3));
for (int c = gzin.read(); c != -1; c = gzin.read()) {
fout.write(c);
}
fout.close();
} catch (IOException ex) {
System.err.println(ex);
}
} else {
System.err.println(args[i] + " does not appear to be a gzipped file.");
}
}
}
}
Unzip a file with GZIPInputStream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPInputStream;
public class Main {
public static void main(String[] args) throws Exception {
int sChunk = 8192;
String zipname = "a.txt.gz";
String source = "a.txt";
FileInputStream in = new FileInputStream(zipname);
GZIPInputStream zipin = new GZIPInputStream(in);
byte[] buffer = new byte[sChunk];
FileOutputStream out = new FileOutputStream(source);
int length;
while ((length = zipin.read(buffer, 0, sChunk)) != -1)
out.write(buffer, 0, length);
out.close();
zipin.close();
}
}