2013-05-11 162 views
0

我已經完成了一些實現,當我創建壓縮文件時,一切正常,壓縮文件已創建,並且它還包含所有文件,而不創建目錄結構。這意味着當我解壓文件時,我有看到所有文件沒有目錄結構。請幫我 把它整理出來..如何壓縮和解壓縮包含相同目錄結構的文件

這裏是我wrriten代碼..

公共無效createZipFolder(字符串路徑){

File dir = new File(path); 
    String list[] = dir.list(); 
    String name = path.substring(path.lastIndexOf("/"), path.length()); 
    String n = path.substring(path.indexOf("/"), path.lastIndexOf("/")); 
    Log.d("JWP", "New Path :"+n); 

    String newPath; 
    if(!dir.canRead() || !dir.canWrite()){ 
     return; 
    } 
    int len = list.length; 

    if(path.charAt(path.length() - 1) != '/'){ 
     newPath = n + "/"; 
    } 
    else{ 
     newPath = n; 
    } 
    try{ 
     ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(newPath + name + ".zip"),BUFFER)); 
     for(int i = 0; i < len; i++){ 
      zip_Folder(new File(path +"/"+ list[i]),zipOut); 
     } 
     zipOut.close(); 
    } 
    catch(FileNotFoundException e){ 
     Log.e("File Not Found", e.getMessage()); 
    } 
    catch(IOException e){ 
     Log.e("IOException", e.getMessage()); 
    }  
} 

private void zip_Folder(File file, ZipOutputStream zipOut)throws IOException { 
    // TODO Auto-generated method stub 
    String s1 = file.getPath(); 
    Log.d("JWP", "PATH "+s1); 
    byte data[] = new byte[BUFFER]; 
    int read; 
    if(file.isFile()){ 
     ZipEntry entry = new ZipEntry(file.getName()); 
     zipOut.putNextEntry(entry); 
     BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); 
     while((read = inputStream.read(data,0,BUFFER)) != -1){ 
      zipOut.write(data, 0, read); 
     } 
     zipOut.closeEntry(); 
     inputStream.close(); 
    } 
    else if(file.isDirectory()){     
      String[] list = file.list(); 
      int len = list.length;     
      for(int i = 0; i < len; i++) 
       zip_Folder(new File(file.getPath()+"/"+ list[i]), zipOut); 
     } 
    } 

這裏是解壓的代碼。 ..

公共無效extractZipFiles(字符串的壓縮文件,字符串DIR){

Log.d("JWP", "ZIP FILE :"+zipFile+ " DIR :"+dir); 
    byte data[] = new byte[BUFFER]; 
    String name,path,zipDir; 
    ZipEntry entry; 
    ZipInputStream zipInputStream; 
    if(!(dir.charAt(dir.length() - 1) == '/')){ 
     dir += "/"; 
    } 
    if(zipFile.contains("/")){ 
     path = zipFile; 
     name = path.substring(path.lastIndexOf("/") + 1, path.length() - 4); 

     zipDir = dir + name + "/"; 
    } 
    else{ 
     path = dir + zipFile; 
     name = path.substring(path.lastIndexOf("/") + 1, path.length() - 4); 
     zipDir = dir + name + "/"; 
    } 
    new File(zipDir).mkdir(); 
    try{ 
     zipInputStream = new ZipInputStream(new FileInputStream(path)); 
     while((entry = zipInputStream.getNextEntry()) != null){ 
      String buildDir = zipDir; 
      String dirs[] = entry.getName().split("/"); 
      if(dirs != null && dirs.length > 0){ 
       for(int i = 0; i < dirs.length - 1; i++){ 
        buildDir += dirs[i] + "/"; 
        new File(buildDir).mkdir(); 
       } 
      } 
      int read = 0; 
      FileOutputStream out = new FileOutputStream(zipDir + entry.getName()); 
      while((read = zipInputStream.read(data, 0, BUFFER)) != -1){ 
       out.write(data, 0, read); 
      } 
      zipInputStream.closeEntry(); 
      out.close(); 
     } 
    } 
    catch(FileNotFoundException e){ 
     e.printStackTrace(); 
    } 
    catch(IOException e){ 
     e.printStackTrace(); 
    } 
} 
+0

你試過這個嗎? http://www.jondev.net/articles/Unzipping_Files_with_Android_%28Programmatically%29 – Hein 2013-05-11 07:45:58

+0

是的,我有,這裏是解壓縮的代碼.. – 2013-05-11 07:49:32

回答

0

您應該將文件夾條目也添加到zip。 也就是說使用目錄名創建ZipEntry(並且不要忘記尾部斜線 - 這對於ZIP歸檔非常重要)。 然後,使用歸檔目錄中的完整相對路徑添加文件條目。

+0

請告訴我任何具體的答案...... – 2013-05-11 09:04:28