2011-09-16 7 views
0

Zip 파일의 내용을 압축 해제하는 코드가 있습니다 (디렉토리가 만들어져 있어야 생성 된 경우).java.util.z iip. * 사용에 관한 몇 가지 쿼리.

그러나 다음을 확인하고 싶습니다. - (1) zip에서 파일을 압축 해제 할 때 파일이 이미 있는지 확인하고 런타임에 사용자가 지정한 값에 따라 해당 파일을 덮어 쓰거나 원본 파일이 남아 있습니다.

특히 지정된 줄 아래에 코드 줄이 있어야합니다. 특정 줄의 파일이 이미 해당 위치에 있는지 확인합니다. 그렇지 않은 경우?

(파일의 존재)이 검사는 아래에 별도로 부여해야한다 직전 코드 줄 ...

copyInputStream(zipFile.getInputStream(entry), 
      new BufferedOutputStream(new FileOutputStream(entry.getName()))); 

(2) 지정된 이름의 디렉토리가 이미 존재하는지 확인하는 방법 또는 아니. 프로그램에 대한 다음과 같은 code--

(new File(entry.getName())).mkdir(); 

전체 코드는 당신이 "존재"라는 단어에 대한 java.io.File API doc를 검색 할 수 있습니다

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


public class Unzip { 

    public static final void copyInputStream(InputStream in, OutputStream out) 
    throws IOException 
    { 
    byte[] buffer = new byte[1024]; 
    int len; 

    while((len = in.read(buffer)) >= 0) 
     out.write(buffer, 0, len); 

    in.close(); 
    out.close(); 
    } 

    public static final void main(String[] args) { 
    Enumeration entries; 
    ZipFile zipFile; 

    if(args.length != 1) { 
     System.err.println("Usage: Unzip zipfile"); 
     return; 
    } 

    try { 
     zipFile = new ZipFile(args[0]); 

     entries = zipFile.entries(); 

     while(entries.hasMoreElements()) { 
     ZipEntry entry = (ZipEntry)entries.nextElement(); 

     if(entry.isDirectory()) { 
      // Assume directories are stored parents first then children. 
      System.err.println("Extracting directory: " + entry.getName()); 
      // This is not robust, just for demonstration purposes. 
      (new File(entry.getName())).mkdir(); 
      continue; 
     } 

     System.err.println("Extracting file: " + entry.getName()); 
     copyInputStream(zipFile.getInputStream(entry), 
      new BufferedOutputStream(new FileOutputStream(entry.getName()))); 
     } 

     zipFile.close(); 
    } catch (IOException ioe) { 
     System.err.println("Unhandled exception:"); 
     ioe.printStackTrace(); 
     return; 
    } 
    } 

} 
+0

정확히 무엇이 필요한 도움에 대해 좀 더 구체적으로 설명해야합니다. 현재, 누군가에게 당신을 위해 당신의 숙제를하라고 요구하는 것 같습니다. –

+0

안녕하세요 ... 질문을 편집하여 정확히 내가 무엇을 요구하는지 명확하게 ... – user893664

+0

이것은 숙제와 매우 흡사합니다. 그렇지? –

답변

1

:

// first obtain the base path where you are running your code 
URL url = getClass().getClassLoader().getResource("."); 

// then check for the existence of the file you need 
File f = new File(url.getPath() + entry.getName()); 

// check for the flag to overwrite the file or it doesn't exist 
if(!file.exists() || overwrite) { 

    copyInputStream(zipFile.getInputStream(entry), 
               new BufferedOutputStream(new FileOutputStream(entry.getName()))); 

} 

폴더의 존재의 확인은 동일한 방법을 사용하여 할 수 있습니다.

3

below- 부여되기 직전에이 검사가 필요합니다.

0

File 클래스는 용도에 맞게 exists, isFileisDirectory 개의 메소드를가집니다. 좀 더 까다로운 (예를 들어, 파일 시스템 추출 위치에 폴더를 가질 수 있습니다

물론
boolean ovrParam = // read flag from args array 

// and inside the loop 
File file = new File(entry.getName()); 
if(ovrParam || !file.exists()) { 
    copyInputStream(zipFile.getInputStream(entry), 
     new BufferedOutputStream(new FileOutputStream(entry.getName()))); 
} 

결합 디렉토리와 파일, 그리고 지퍼가있는 파일을 포함

그냥 같은 것을 사용 압축을 풀 폴더의 이름과 정확하게 동일하거나 반대의 이름). 그러나 어쨌든 이것은 당신에게 지침을 줄 것입니다. 당신은 폴더 또는 디렉토리는 다음과 같이 java.io.File을 사용하여 존재하는지 확인해야합니다

건배,

관련 문제