java.util.zip.ZipFile.getInputStream(ZipEntry entry)方法範例

2019-10-16 22:31:59

java.util.zip.ZipFile.getInputStream(ZipEntry entry)方法返回一個輸入流,用於讀取指定zip檔案條目的內容。

宣告

以下是java.util.zip.ZipFile.getInputStream(ZipEntry entry)方法的宣告。

public InputStream getInputStream(ZipEntry entry)
   throws IOException

引數

  • entry - zip檔案條目。

返回值

  • 用於讀取指定zip檔案條目內容的輸入流。

異常

  • ZipException - 如果發生ZIP格式錯誤。
  • IOException - 如果發生I/O錯誤。
  • IllegalStateException - 如果發生I/O錯誤。

前提條件

D:/test>目錄下用以下內容建立一個檔案Hello.txt。

This is an example.

範例

以下範例顯示了java.util.zip.ZipFile.getInputStream(ZipEntry entry)方法的用法。

package com.yiibai;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ZipFileDemo {
   private static String SOURCE_FILE = "D:\\test\\Hello.txt";
   private static String TARGET_FILE = "D:\\test\\Hello.zip";

   public static void main(String[] args) {
      try {
         createZipFile();
         readZipFile();
      } catch(IOException ioe) {
         System.out.println("IOException : " + ioe);
      }
   }

   private static void createZipFile() throws IOException{
      FileOutputStream fout = new FileOutputStream(TARGET_FILE);
      CheckedOutputStream checksum = new CheckedOutputStream(fout, new Adler32());
      ZipOutputStream zout = new ZipOutputStream(checksum);

      FileInputStream fin = new FileInputStream(SOURCE_FILE);
      ZipEntry zipEntry = new ZipEntry(SOURCE_FILE);
      zipEntry.setComment("This is a sample file.");
      zout.putNextEntry(zipEntry);
      int length;
      byte[] buffer = new byte[1024];
      while((length = fin.read(buffer)) > 0) {
         zout.write(buffer, 0, length);
      }

      zout.closeEntry();
      fin.close();
      zout.close();
   }

   private static void readZipFile() throws IOException{
      final ZipFile file = new ZipFile(TARGET_FILE); 

      try { 
         ZipEntry entry = file.getEntry(SOURCE_FILE);
         if(entry != null){
            System.out.printf("File: %s Size %d Modified on %TD %n", 
               entry.getName(), entry.getSize(), 
               new Date(entry.getTime()));
            extractFile(entry, file.getInputStream(entry)); 
         }
         System.out.printf("Zip file %s extracted successfully.", SOURCE_FILE); 
      } 
      finally { 
         file.close(); 
      }
   }

   private static void extractFile(final ZipEntry entry, InputStream is) 
      throws IOException {
      FileOutputStream fos = null; 
      try { 
         fos = new FileOutputStream(entry.getName()); 
         final byte[] buf = new byte[1024]; 
         int read = 0; 
         int length; 
         while ((length = is.read(buf, 0, buf.length)) >= 0) { 
            fos.write(buf, 0, length); 
         } 
      } catch (IOException ioex) { 
         fos.close(); 
      } 
   }
}

編譯並執行上面的程式,產生以下結果 -

File: D:\test\Hello.txt Size 1024 Modified on 05/22/17 
Zip file D:\test\Hello.txt extracted successfully.