다음 코드를 사용하여 Java에서 zip 파일을 추출합니다.java에서 압축 된 폴더를 어떻게 추출해야합니까?
import java.io.*;
import java.util.zip.*;
class testZipFiles
{
public static void main(String[] args)
{
try
{
String filename = "C:\\zip\\includes.zip";
testZipFiles list = new testZipFiles();
list.getZipFiles(filename);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void getZipFiles(String filename)
{
try
{
String destinationname = "c:\\zip\\";
byte[] buf = new byte[1024];
ZipInputStream zipinputstream = null;
ZipEntry zipentry;
zipinputstream = new ZipInputStream(
new FileInputStream(filename));
zipentry = zipinputstream.getNextEntry();
while (zipentry != null)
{
//for each entry to be extracted
String entryName = zipentry.getName();
System.out.println("entryname "+entryName);
int n;
FileOutputStream fileoutputstream;
File newFile = new File(entryName);
String directory = newFile.getParent();
if(directory == null)
{
if(newFile.isDirectory())
break;
}
fileoutputstream = new FileOutputStream(
destinationname+entryName);
while ((n = zipinputstream.read(buf, 0, 1024)) > -1)
fileoutputstream.write(buf, 0, n);
fileoutputstream.close();
zipinputstream.closeEntry();
zipentry = zipinputstream.getNextEntry();
}//while
zipinputstream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
분명히 break 문으로 인해 폴더 트리가 추출되지 않습니다. 재귀를 사용하여 폴더 트리를 처리하려고했지만 실패했습니다. 누군가가이 코드를 개선하여 압축 된 단일 레벨 폴더 대신 폴더 트리를 처리하는 방법을 보여줄 수 있습니까?
Chathuranga : Emre의 대답으로 문제가 해결 되었습니까? 그렇다면 그의 대답을 확인해 주셔서 감사합니다 =) – mikek