2016-11-28 5 views
1

내 응용 프로그램은 압축 파일을 다운로드해야하며 응용 프로그램 폴더에서 압축을 해제해야합니다. 문제는 zip에 파일이 있지만 폴더가없고 각 폴더에 다른 파일이 있다는 것입니다. 나는 같은 구조를 유지할 것이지만 나는 그것을 어떻게하는지 모른다. 나는 파일들을 압축해서 만들었지 만 폴더들을 압축하지 않으면 성공한다. 어떻게 할 수 있는지 아는 사람이 있습니까? 많은 감사.android에서 압축 해제 폴더

+0

https://github.com/commonsguy/cwac-security/#usage-ziputils – CommonsWare

답변

3

ZIP 아카이브의 각 디렉토리 항목에 대한 디렉토리를 만들어야합니다.

/** 
* Unzip a ZIP file, keeping the directory structure. 
* 
* @param zipFile 
*  A valid ZIP file. 
* @param destinationDir 
*  The destination directory. It will be created if it doesn't exist. 
* @return {@code true} if the ZIP file was successfully decompressed. 
*/ 
public static boolean unzip(File zipFile, File destinationDir) { 
    ZipFile zip = null; 
    try { 
    destinationDir.mkdirs(); 
    zip = new ZipFile(zipFile); 
    Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); 
    while (zipFileEntries.hasMoreElements()) { 
     ZipEntry entry = zipFileEntries.nextElement(); 
     String entryName = entry.getName(); 
     File destFile = new File(destinationDir, entryName); 
     File destinationParent = destFile.getParentFile(); 
     if (destinationParent != null && !destinationParent.exists()) { 
     destinationParent.mkdirs(); 
     } 
     if (!entry.isDirectory()) { 
     BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); 
     int currentByte; 
     byte data[] = new byte[DEFUALT_BUFFER]; 
     FileOutputStream fos = new FileOutputStream(destFile); 
     BufferedOutputStream dest = new BufferedOutputStream(fos, DEFUALT_BUFFER); 
     while ((currentByte = is.read(data, 0, DEFUALT_BUFFER)) != EOF) { 
      dest.write(data, 0, currentByte); 
     } 
     dest.flush(); 
     dest.close(); 
     is.close(); 
     } 
    } 
    } catch (Exception e) { 
    return false; 
    } finally { 
    if (zip != null) { 
     try { 
     zip.close(); 
     } catch (IOException ignored) { 
     } 
    } 
    } 
    return true; 
} 
+0

좋은 일 다음은 디렉토리 구조를 유지하는 내가 쓴 방법과 사용이다. 나를 구원해. – Abhishek