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;
}
}
}
정확히 무엇이 필요한 도움에 대해 좀 더 구체적으로 설명해야합니다. 현재, 누군가에게 당신을 위해 당신의 숙제를하라고 요구하는 것 같습니다. –
안녕하세요 ... 질문을 편집하여 정확히 내가 무엇을 요구하는지 명확하게 ... – user893664
이것은 숙제와 매우 흡사합니다. 그렇지? –