2011-09-04 9 views
5

SUN 사이트 (http://java.sun.com/developer/technicalArticles/Programming/compression/)에서 예제를 찾았지만 BufferedOutputStream을 반환합니다. 하지만 ZipEntry 파일을 InputStream으로 가져 와서 다음 파일을 처리하고 싶습니다. 그게 가능하니? 내 프로그램은 하드 디스크에 접근 할 수 없으므로 임시로 파일을 저장할 수 없습니다.Java로 Zip 파일에서 메모리로 파일을 읽으려면 어떻게해야합니까?

import java.io.*; 
import java.util.zip.*; 

public class UnZip { 
    final int BUFFER = 2048; 
    public static void main (String argv[]) { 
     try { 
     BufferedOutputStream dest = null; 
     FileInputStream fis = new 
     FileInputStream(argv[0]); 
     ZipInputStream zis = new 
     ZipInputStream(new BufferedInputStream(fis)); 
     ZipEntry entry; 
     while((entry = zis.getNextEntry()) != null) { 
      System.out.println("Extracting: " +entry); 
      int count; 
      byte data[] = new byte[BUFFER]; 
      // write the files to the disk 
      FileOutputStream fos = new 
      FileOutputStream(entry.getName()); 
      dest = new 
       BufferedOutputStream(fos, BUFFER); 
      while ((count = zis.read(data, 0, BUFFER)) 
       != -1) { 
       dest.write(data, 0, count); 
      } 
      dest.flush(); 
      dest.close(); 
     } 
     zis.close(); 
     } catch(Exception e) { 
     e.printStackTrace(); 
     } 
    } 
} 

답변

13

글쎄, 파일을 쓰는 부분을 데이터로하고 싶은 부분으로 변경하십시오.

while((entry = zis.getNextEntry()) != null) { 
    System.out.println("Extracting: " + entry); 
    int count; 
    byte data[] = new byte[BUFFER]; 
    string filename = entry.getName(); 
    System.out.println("Filename: " + filename); 
    while ((count = zis.read(data, 0, BUFFER)) != -1) { 
     // Do whatever you want with the data variable 
     System.out.println(data); 
    } 
} 
+0

바이트 [] 데이터 = 새 바이트 [버퍼]; –

관련 문제