중첩 된 zip 아카이브에서 파일을 추출하고 메모리에서 처리하려고합니다. 이 질문은 무엇임시 파일을 사용하지 않고 Java로 중첩 된 zip 파일에서 데이터를 읽는 방법?
하지에 대한 :
어떻게 자바로 zip 파일을 읽는: NO, 질문 등 지퍼 내에서 압축 파일 내에서 zip 파일을 읽는 방법입니다 등등 (중첩 된 zip 파일에서와 같이).
임시 결과를 디스크에 씁니다. 아니요, 모든 것을 기억하고 있습니다. 결과를 디스크에 일시적으로 기록하는 비효율적 인 방법을 사용하여 많은 답변을 찾았지만 원하는 것은 아닙니다.
예 :
ZipFile를 -> Zipfile1 -> Zipfile2 -> Zipfile3
목표 : 중첩 ZIP 파일들 각각에있는 데이터를 모두 메모리 및 사용을 추출 자바.
ZipFile 대답입니까, 그렇습니까? > Zipfile1
하지만 일단 Zipfile2에 도착하고, 수행 -
ZipFile를 :
을 아니, 그것은을 위해, 그것은 첫 번째 반복을 위해 작동하지 않습니다ZipInputStream z = new ZipInputStream(zipFile.getInputStream(zipEntry)) ;
NullPointerException이 발생합니다.
내 코드 :
public class ZipHandler { String findings = new String(); ZipFile zipFile = null; public void init(String fileName) throws AppException{ try { //read file into stream zipFile = new ZipFile(fileName); Enumeration<?> enu = zipFile.entries(); exctractInfoFromZip(enu); zipFile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //The idea was recursively extract entries using ZipFile public void exctractInfoFromZip(Enumeration<?> enu) throws IOException, AppException{ try { while (enu.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) enu.nextElement(); String name = zipEntry.getName(); long size = zipEntry.getSize(); long compressedSize = zipEntry.getCompressedSize(); System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n", name, size, compressedSize); // directory ? if (zipEntry.isDirectory()) { System.out.println("dir found:" + name); findings+=", " + name; continue; } if (name.toUpperCase().endsWith(".ZIP") || name.toUpperCase().endsWith(".GZ")) { String fileType = name.substring( name.lastIndexOf(".")+1, name.length()); System.out.println("File type:" + fileType); System.out.println("zipEntry: " + zipEntry); if (fileType.equalsIgnoreCase("ZIP")) { //ZipFile here returns a NULL pointer when you try to get the first nested zip ZipInputStream z = new ZipInputStream(zipFile.getInputStream(zipEntry)) ; System.out.println("Opening ZIP as stream: " + name); findings+=", " + name; exctractInfoFromZip(zipInputStreamToEnum(z)); } else if (fileType.equalsIgnoreCase("GZ")) { //ZipFile here returns a NULL pointer when you try to get the first nested zip GZIPInputStream z = new GZIPInputStream(zipFile.getInputStream(zipEntry)) ; System.out.println("Opening ZIP as stream: " + name); findings+=", " + name; exctractInfoFromZip(gZipInputStreamToEnum(z)); } else throw new AppException("extension not recognized!"); } else { System.out.println(name); findings+=", " + name; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Findings " + findings); } public Enumeration<?> zipInputStreamToEnum(ZipInputStream zStream) throws IOException{ List<ZipEntry> list = new ArrayList<ZipEntry>(); while (zStream.available() != 0) { list.add(zStream.getNextEntry()); } return Collections.enumeration(list); }
_ "(곧 수정 예정)"_ - 부분 질문을 게시하지 마십시오. 게시하기 전에 공식 질문이있을 때까지 기다리십시오. –
주된 문제는 먼저 ZipInputStream에서'getNextEntry()'를 사용하여 올바른 zip 항목을 찾아야한다는 것입니다. – JMax
wronlgy 게시에 대한 죄송합니다. 내가 직장에서 나올 때 게시물을 수정 해 줄거야. 나는 이걸 가지고 정말로 붙어있다. –