Xử lý Zip file trong Java

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

Nôi dung sampleZipFile.txt

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

Tạo class SampleZipFile.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.ZipOutputStream;

public class SampleZipFile {

  /**
  * Create by Cuder
  */
  public static void main(String[] args) {
  ZipOutputStream zipOutputStream = null;
  FileInputStream fileInputStream = null;
  try {
    File fileInput = new File("D:\\eclipse4.4\\workspace\\SampleJava\\resource\\sampleZipFile.txt");
    File fileOutput = new File("D:\\eclipse4.4\\workspace\\SampleJava\\resource\\sampleZipFile.zip");
    FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
    zipOutputStream = new ZipOutputStream(fileOutputStream);
    fileInputStream = new FileInputStream(fileInput);
    ZipEntry entry = new ZipEntry(fileInput.getName());
    zipOutputStream.putNextEntry(entry);
    byte[] buf = new byte[1024];
    int byteRead = 0;
    while ((byteRead = fileInputStream.read(buf)) > 0) {
      zipOutputStream.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 != fileInputStream) {
        fileInputStream.close();
      }
      if (null != zipOutputStream) {
        zipOutputStream.close();
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    }
  }
}

Kết quả sampleZipFile.zip

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

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

Nôi dung sampleZipFile.txt

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

Tạo class SampleZipFileWithCharset.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.ZipOutputStream;

public class SampleZipFileWithCharset {

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

Kết quả sampleZipFile.zip

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

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

Nôi dung sampleZipFile.txt

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

Tạo class SampleZipFileWithCharset.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.ZipOutputStream;

public class SampleZipFileWithResourceTryCatch {

  /**
  * 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.txt");
    File fileOutput = new File("D:\\eclipse4.4\\workspace\\SampleJava\\resource\\sampleZipFile.zip");
    try (FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
      ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream, charset);
      FileInputStream fileInputStream = new FileInputStream(fileInput);) {
      ZipEntry entry = new ZipEntry(fileInput.getName());
      zipOutputStream.putNextEntry(entry);
      byte[] buf = new byte[1024];
      int byteRead = 0;
      while ((byteRead = fileInputStream.read(buf)) > 0) {
        zipOutputStream.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ả sampleZipFile.zip

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

5 (100%) 1 vote
Click Here to Leave a Comment Below 0 comments