Xử lý UnZip file trong Java

1. Xử lý UnZip file cơ bản

Nôi dung sampleZipFile.zip

line content 1
line content 2
line content 3
line content 4
line content 5
 line content 6

Tạo class SampleUnzipFile.java

package com.loop.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class SampleUnzipFile {

  /**
  * Create by Cuder
  */
  public static void main(String[] args) {
    ZipInputStream zipInputStream = null;
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
      File fileInput = new File("D:\\eclipse4.4\\workspace\\SampleJava\\resource\\sampleZipFile.zip");
      File fileOutput = new File("D:\\eclipse4.4\\workspace\\SampleJava\\resource\\sampleZipFile1.txt");
      fileOutputStream = new FileOutputStream(fileOutput);
      fileInputStream = new FileInputStream(fileInput);
      zipInputStream = new ZipInputStream(fileInputStream);
      ZipEntry entry = zipInputStream.getNextEntry();
      if (null != entry) {
        byte[] buf = new byte[1024];
        int byteRead = 0;
        while ((byteRead = zipInputStream.read(buf)) > 0) {
          fileOutputStream.write(buf, 0, byteRead);
        }
      }
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        if (null != fileOutputStream) {
          fileOutputStream.close();
        }
        if (null != zipInputStream) {
          zipInputStream.close();
        }
        if (null != fileInputStream) {
          fileInputStream.close();
        }
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
}

Kết quả sampleZipFile1.txt

line content 1
line content 2
line content 3
line content 4
line content 5
 line content 6

2. Xử lý UnZip file với Charset

Nôi dung sampleZipFile.zip

line content 1
line content 2
line content 3
line content 4
line content 5
 line content 6

Tạo class SampleUnzipFileWithCharset.java

package com.loop.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class SampleUnzipFileWithCharset {

  /**
  * Create by Cuder
  */
  public static void main(String[] args) {
    ZipInputStream zipInputStream = null;
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    Charset charset = Charset.forName("UTF-8");
    try {
      File fileInput = new File("D:\\eclipse4.4\\workspace\\SampleJava\\resource\\sampleZipFile.zip");
      File fileOutput = new File("D:\\eclipse4.4\\workspace\\SampleJava\\resource\\sampleZipFile2.txt");
      fileOutputStream = new FileOutputStream(fileOutput);
      fileInputStream = new FileInputStream(fileInput);
      zipInputStream = new ZipInputStream(fileInputStream, charset);
      ZipEntry entry = zipInputStream.getNextEntry();
      if (null != entry) {
        byte[] buf = new byte[1024];
        int byteRead = 0;
        while ((byteRead = zipInputStream.read(buf)) > 0) {
          fileOutputStream.write(buf, 0, byteRead);
        }
      }
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        if (null != fileOutputStream) {
          fileOutputStream.close();
        }
        if (null != zipInputStream) {
          zipInputStream.close();
        }
        if (null != fileInputStream) {
          fileInputStream.close();
        }
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
}

Kết quả sampleZipFile2.txt

line content 1
line content 2
line content 3
line content 4
line content 5
 line content 6

3. Xử lý UnZip file với try catch resource ( JDK7)

Nôi dung sampleZipFile.zip

line content 1
line content 2
line content 3
line content 4
line content 5
 line content 6

Tạo class SampleUnzipFileWithResourceTryCatch.java

package com.loop.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class SampleUnzipFileWithResourceTryCatch {

  /**
  * Create by Cuder
  */
  public static void main(String[] args) {
    Charset charset = Charset.forName("UTF-8");
    File fileInput = new File("D:\\eclipse4.4\\workspace\\SampleJava\\resource\\sampleZipFile.zip");
    File fileOutput = new File("D:\\eclipse4.4\\workspace\\SampleJava\\resource\\sampleZipFile3.txt");
    try (FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
      FileInputStream fileInputStream = new FileInputStream(fileInput);
      ZipInputStream zipInputStream = new ZipInputStream(
      fileInputStream, charset);) {
      ZipEntry entry = zipInputStream.getNextEntry();
      if (null != entry) {
        byte[] buf = new byte[1024];
        int byteRead = 0;
        while ((byteRead = zipInputStream.read(buf)) > 0) {
          fileOutputStream.write(buf, 0, byteRead);
        }
      }
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

Kết quả sampleZipFile3.txt

line content 1
line content 2
line content 3
line content 4
line content 5
 line content 6

Rate this post
Click Here to Leave a Comment Below 0 comments