62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPInputStream;
public class GZIPcompress {
public static void main(String[] args) {
args[0]="aaaa.gz";
try{
if (args.length == 0) {
System.out.println("usage: java expandgz filename.gzzzzzzzzzz");
System.exit(0); }
GZIPInputStream gzi = new GZIPInputStream(new FileInputStream(args[0]));
int to = args[0].lastIndexOf('.');
if (to == -1) {
System.out.println("usage: java expandgz filename.gz");
System.exit(0); }
String fout = args[0].substring(0, to);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(fout) );
System.out.println("writing " + fout);
int b;
do {
b = gzi.read();
if (b==-1) break;
bos.write(b);
} while (true);
gzi.close();
bos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
// Nothing in the buffer. We need to know whether or not
// there is unread data available in the underlying stream
// since the base class will not handle an empty file.
// Read a byte to see if there is data and if so,
// push it back onto the stream...
byte[] b=new byte[1];
int ret=in.read(b,0,1);
if (ret==-1)
{
eos=true;
return -1;
}
同时我把重写的那个类贴出来 请不要走弯路
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.io.IOException;
public
class MultiMemberGZIPInputStream extends GZIPInputStream {
public MultiMemberGZIPInputStream(InputStream in, int size) throws IOException
{
// Wrap the stream in a PushbackInputStream...
super(new PushbackInputStream(in, size), size);
this.size=size;
}
public MultiMemberGZIPInputStream(InputStream in) throws IOException
{
// Wrap the stream in a PushbackInputStream...
super(new PushbackInputStream(in, 1024));
this.size=-1;
}
private MultiMemberGZIPInputStream(MultiMemberGZIPInputStream parent) throws IOException
{
super(parent.in);
this.size=-1;
this.parent=parent.parent==null ? parent : parent.parent;
this.parent.child=this;
}
private MultiMemberGZIPInputStream(MultiMemberGZIPInputStream parent, int size) throws IOException
{
super(parent.in, size);
this.size=size;
this.parent=parent.parent==null ? parent : parent.parent;
this.parent.child=this;
}
private MultiMemberGZIPInputStream parent;
private MultiMemberGZIPInputStream child;
private int size;
private boolean eos;
public int read(byte[] inputBuffer, int inputBufferOffset, int inputBufferLen) throws IOException {
if (eos) { return -1;}
if (this.child!=null)
return this.child.read(inputBuffer, inputBufferOffset, inputBufferLen);
int charsRead=super.read(inputBuffer, inputBufferOffset, inputBufferLen);
if (charsRead==-1)
{
// Push any remaining buffered data back onto the stream
// If the stream is then not empty, use it to construct
// a new instance of this class and delegate this and any
// future calls to it...
int n = inf.getRemaining() - 8;
if (n > 0)
{
// More than 8 bytes remaining in deflater
// First 8 are gzip trailer. Add the rest to
// any un-read data...
((PushbackInputStream)this.in).unread(buf, len-n, n);
}
else
{
// Nothing in the buffer. We need to know whether or not
// there is unread data available in the underlying stream
// since the base class will not handle an empty file.
// Read a byte to see if there is data and if so,
// push it back onto the stream...
byte[] b=new byte[1];
int ret=in.read(b,0,1);
if (ret==-1)
{
eos=true;
return -1;
}
else
((PushbackInputStream)this.in).unread(b, 0, 1);
}
MultiMemberGZIPInputStream child;
if (this.size==-1)
child=new MultiMemberGZIPInputStream(this);
else
child=new MultiMemberGZIPInputStream(this, this.size);
return child.read(inputBuffer, inputBufferOffset, inputBufferLen);
}
else
return charsRead;
}
}
package com.question;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* GZIPInputStream question.
*
* @date 2008-1-8
* @author Xxx
*/
public class Q2 {
// GZ file name : eg. C:\\demo.gz
private static final String GZ_FILE_NAME = "C:\\demo.gz";
// the string will be writed in the file
private static final String TEST_STRING = "Test .Test .Test .Test .Test .Test .Test .";
// this String is used for naming the gz file
private static final String FROM_FILE_NAME = "C:\\demo.gz";
// this String is used for naming the log file(according to the user's need, you can modify it.).
private static final String TO_FILE_NAME = "c:\\app.log";
/**
* this method only for init a gz file.
* if you hava a gz file, need not invode it.
* Note: the generated file is stored "C:\\demo.gz"
*/
@Deprecated
public void writeFile() {
try {
StringBuilder sb = new StringBuilder(TEST_STRING);
/*
* populate String
*/
for (int i = 0; i < 15; i ++) {
sb.append(sb);
}
String output = sb.toString();
File file = new File(GZ_FILE_NAME);
if (!file.exists()) {
file.createNewFile();
}
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
GZIPOutputStream out = new GZIPOutputStream(outputStream);
out.write(output.getBytes());
// TODO only for test. so close in this place.
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* Main method.
*
* @param args
*/
public static void main(String[] args) {
try {
/*
* init file . get a gz file.
*/
// TODO
// Q2 q2 = new Q2();
// q2.writeFile();
/*
* create file
*/
File inputFile = new File(FROM_FILE_NAME);
if (!inputFile.exists()) {
inputFile.createNewFile();
}
FileInputStream inputStream = new FileInputStream(inputFile);
GZIPInputStream gzip = new GZIPInputStream(inputStream);
/*
* create file
*/
File outputFile = new File(TO_FILE_NAME);
if (!outputFile.exists()) {
outputFile.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
/*
* write file
*/
int b;
while((b = gzip.read()) != -1) {
bos.write(b);
}
// only for test, should be close in a correct way(eg. finally ).
gzip.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.question;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPInputStream;
public class Q2 {
public static void main(String[] args) {
args = new String[1];
args[0] = "c:/aaaa.gz";
try {
if (args.length == 0) {
System.out.println("usage: java expandgz filename.gzzzzzzzzzz");
System.exit(0);
}
GZIPInputStream gzi = new GZIPInputStream(new FileInputStream(args[0]));
int to = args[0].lastIndexOf('.');
if (to == -1) {
System.out.println("usage: java expandgz filename.gz");
System.exit(0);
}
String fout = args[0].substring(0, to);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fout));
System.out.println("writing " + fout);
int b;
do {
b = gzi.read();
if (b == -1)
break;
bos.write(b);
} while (true);
gzi.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try
{
int nnumber;
FileInputStream fin=new FileInputStream("c:\\abc\\SG_main__2451110060000.log.done.gz"); GZIPInputStream gzin=new GZIPInputStream(fin);
FileOutputStream fout=new FileOutputStream("c:\\abc\\cc.log");
byte[] buf=new byte[1024];
while ((nnumber=gzin.read(buf,0,buf.length)) != -1)
fout.write(buf,0,nnumber);
gzin.close();
fout.close();
fin.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}