GZip 압축 풀기 도구 클래스

package test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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;

import org.apache.log4j.Logger;


public class GZipUtil {
 private static Logger log = Logger.getLogger(GZipUtil.class);
    public GZipUtil() {
    }
    /**
     *      GZip  
     *
     */
    private byte[] gzipFile(String src) {
     String fileName = "e:\\xml\\1.txt";
//     String fileName = src;
  //     
  FileInputStream zipin;
  ByteArrayOutputStream bin = new ByteArrayOutputStream();
  try {
   zipin = new FileInputStream(fileName);
   fileName = "e:\\xml\\1.txt.zip";
   File zip = new File(fileName);
   GZIPOutputStream zipout = new GZIPOutputStream(
     new FileOutputStream(zip));
   byte[] buffer = new byte[4096];
   int bytes_read;
   // 1.txt     buffer 
   while ((bytes_read = zipin.read(buffer)) != -1) {
    //  buffer     ,   1.txt.zip 
    zipout.write(buffer, 0, bytes_read);
   }
   zipout.flush();
   zipout.finish();
   //     
   FileInputStream fos = new FileInputStream(new File(fileName));
   buffer = new byte[4096];
   int flag = 0;
   while ((flag = fos.read(buffer)) != -1) {
    // 1.txt.zip   bin 
    bin.write(buffer, 0, flag);
   }
   fos.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return bin.toByteArray();
    }
    
    /**
     *      GZip   
     *  zipFileName   xmlFileName
     *
     */
    private void unGzipFile(byte[] in,String name) {
     
     String zipFileName = "e:\\xml\\" + name + ".txt.zip";
  String xmlFileName = "e:\\xml\\" + name + ".txt";
//  String zipFileName1 = "e:\\xml\\" + name + "_1.txt.zip";
//  String xmlFileName1 = "e:\\xml\\" + name + "_1.txt";
  
  //      
  byte[] b = in;
  int flag = 0;
  if (b != null) {
   //   ZIP  
   ByteArrayInputStream bin = new ByteArrayInputStream(b);
   FileOutputStream fos;
   try {
    fos = new FileOutputStream(new File(zipFileName));
    byte[] buffer = new byte[4096];
    while ((flag = bin.read(buffer)) != -1) {
     fos.write(buffer, 0, flag);
    }
    fos.close();

    //   zip  
    FileOutputStream foszip = new FileOutputStream(
      new File(xmlFileName));
    GZIPInputStream zin = new GZIPInputStream(new FileInputStream(
      new File(zipFileName)));
    buffer = new byte[4096];
    flag = 0;
    while ((flag = zin.read(buffer)) != -1) {
     foszip.write(buffer, 0, flag);
    }
    foszip.close();
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }

    }
    
}

좋은 웹페이지 즐겨찾기