2014-07-18 2 views
0

아래 코드를 사용하여 zip 파일의 내용을 추출하는 데 문제가 없습니다. 최근에는 zip 파일에있는 zip 파일의 압축을 풀어야한다는 요구 사항이있어 처리 논리가 올바르지 않게되었습니다. 나는 in.vision을 통해 zip 파일을 읽고, .log를 포함하는 모든 파일의 배열을 채 웁니다. 제 질문은 다른 zip 파일이있을 때 루틴을 호출하는 방법입니다.java에서 zip 파일 내의 zip 파일의 압축을 해제하는 방법

try 
    { 
     ZipInputStream zipinputstream = null; 
     ZipEntry zipentry; 
     ZipFile zf = new ZipFile(passed_ZipFile); 
     ZipFile zf = new ZipFile("c:\\temp\\myzipFile.zip"); 
     Enumeration entries = zf.entries(); 
     zipinputstream = new ZipInputStream(new FileInputStream(passed_ZipFile)); 
     zipentry = zipinputstream.getNextEntry(); 
     while (zipentry != null) 
     { 
      String entryName = filename; 
      zipentry = (ZipEntry) entries.nextElement(); 
      RandomAccessFile rf; 
      File newFile = new File(entryName); 
      String directory = newFile.getParent(); 
      if(directory == null) 
      { 
       if(newFile.isDirectory()) 
       { 
        zipentry = zipinputstream.getNextEntry(); 
        break; 
       } 
      } 
      filename = zipentry.getName(); 

      if (zipentry.getName().contains(".log")) 
      { 
       // Perform log processing 
      } 
      else if (zipentry.getName().contains(".zip")) 
      { 
       Call same routine passing the zip file - The routine will then process all files in the zip file 
      } 
+0

루틴의 이름은 무엇입니까? 또한이 코드에서 파일 이름을 가져 오는 것만으로는 추출되지 않습니다. 너 어디에서 그런거야? –

답변

1

This 당신이 반복적으로 압축 된 파일의 압축을 풀 수있는 방법입니다.

package com.test; 

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.util.Enumeration; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipFile; 

public class Unzipper { 
    private final static int BUFFER_SIZE = 2048; 
    private final static String ZIP_FILE = "/home/anton/test/test.zip"; 
    private final static String DESTINATION_DIRECTORY = "/home/anton/test/"; 
    private final static String ZIP_EXTENSION = ".zip"; 

    public static void main(String[] args) { 
    System.out.println("Trying to unzip file " + ZIP_FILE); 
     Unzipper unzip = new Unzipper(); 
     if (unzip.unzipToFile(ZIP_FILE, DESTINATION_DIRECTORY)) { 
     System.out.println("Succefully unzipped to the directory " 
      + DESTINATION_DIRECTORY); 
     } else { 
     System.out.println("There was some error during extracting archive to the directory " 
      + DESTINATION_DIRECTORY); 
     } 
    } 

public boolean unzipToFile(String srcZipFileName, 
    String destDirectoryName) { 
    try { 
    BufferedInputStream bufIS = null; 
    // create the destination directory structure (if needed) 
    File destDirectory = new File(destDirectoryName); 
    destDirectory.mkdirs(); 

    // open archive for reading 
    File file = new File(srcZipFileName); 
    ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ); 

    //for every zip archive entry do 
    Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries(); 
    while (zipFileEntries.hasMoreElements()) { 
    ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); 
    System.out.println("\tExtracting entry: " + entry); 

    //create destination file 
    File destFile = new File(destDirectory, entry.getName()); 

    //create parent directories if needed 
    File parentDestFile = destFile.getParentFile(); 
    parentDestFile.mkdirs(); 

    if (!entry.isDirectory()) { 
    bufIS = new BufferedInputStream(
     zipFile.getInputStream(entry)); 
    int currentByte; 

    // buffer for writing file 
    byte data[] = new byte[BUFFER_SIZE]; 

    // write the current file to disk 
    FileOutputStream fOS = new FileOutputStream(destFile); 
    BufferedOutputStream bufOS = new BufferedOutputStream(fOS, BUFFER_SIZE); 

    while ((currentByte = bufIS.read(data, 0, BUFFER_SIZE)) != -1) { 
     bufOS.write(data, 0, currentByte); 
    } 

    // close BufferedOutputStream 
    bufOS.flush(); 
    bufOS.close(); 

    // recursively unzip files 
    if (entry.getName().toLowerCase().endsWith(ZIP_EXTENSION)) { 
     String zipFilePath = destDirectory.getPath() + File.separatorChar + entry.getName(); 

     unzipToFile(zipFilePath, zipFilePath.substring(0, 
       zipFilePath.length() - ZIP_EXTENSION.length())); 
    } 
    } 
    } 
    bufIS.close(); 
    return true; 
    } catch (Exception e) { 
    e.printStackTrace(); 
    return false; 
    } 
} 
} 
+0

그러나 내부 파일의 압축을 풀기 전에 외부 파일을 완전히 압축 해제 할 필요는 없습니다. 당신은 당신이 안쪽 항목을 찾을 때까지 바깥 쪽 우편 번호를 읽을 수 있고, * 그 *를 압축 해제 할 수 있습니다. – tucuxi

+0

귀하의 조언을 주신 모든 분들께 감사드립니다. –

관련 문제