Làm thế sử dụng lớp ZipInputStream nào để giải nén một tập tin trong java? (How to unzip file using class ZipInputStream in Java?)






Như bài trước tôi đã trình bày cách sử dụng ZipOutputStream để nén(zip) một tập tin thành file, trong bài viết này tôi sẽ hướng dẫn cách sử dụng lớp ZipInputStream để giải nén một tập tin zip(lưu ý tập tin này không thiết lập password) vì 2 lớp này không hỗ trợ xử lý các zip có password.





Đầu tiên ta cần chuẩn bị một tập tin zip để thực hiện bài ví dụ này, như trong hình tôi có một file zip tên "anime_chibi_girl.zip" như sau.


Và tôi sẽ giải nén tập tin trên bằng mã bên dưới:

/**
 * @(#)UnZipWithZipInputStream.java
 *
 * UnZipWithZipInputStream application
 *
 * @author developer.bnson@gmail.com
 * @version 1.00 2014/3/9
 */

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

public class UnZipWithZipInputStream {
   
    public static void main(String[] args) {

        UnZipWithZipInputStream runMain = new UnZipWithZipInputStream();
        runMain.unZip("D:\\Z-Test\\Demo\\anime_chibi_girl.zip", "D:\\Z-Test\\Demo\\");
    }
   

    public void unZip(String zipPath, String outputFolder) {

        byte[] buffer = new byte[1024];

        try {

            //Create output directory is not exists
            //Tạo folder nếu folder đó không tồn tại.
            File folder = new File(outputFolder);
           
            if (!folder.exists()) {
                folder.mkdir();
            }

            //Get the zip file content.
            //Lấy nội dụng trong tập tin zip.
            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipPath));
            //Get the zipped file list entry.
            //Lấy danh sách tập tin được nén.
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {

                String fileName = ze.getName();
                File newFile = new File(outputFolder + File.separator + fileName);

                System.out.println("File unzip : " + newFile.getAbsoluteFile());

                //Create all non exists folders.
                //Khởi tạo tất cả các folder không tồn tại.
                new File(newFile.getParent()).mkdirs();

                FileOutputStream fos = new FileOutputStream(newFile);

                int len;
                //Read and write all zipped file is list.
                //Đọc và ghi các tập tin được nén trong danh sách.
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
                ze = zis.getNextEntry();
            }

            zis.closeEntry();
            zis.close();

            System.out.println("Complete.");

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }   
   
   
}

Kết quả sau khi chương trình được thực thi sẽ là:

File unzip : D:\Z-Test\Demo\anime_chibi_girl.jpg
Complete.

Quay trở lại thư mục giải nén(unzip) ta sẽ thấy được kết quả như sau:































No comments:

Post a Comment