2012-09-24 195 views
0

我可以壓縮包含在特定文件夾中的文件。這是我使用的代碼:如何在Android中壓縮特定文件夾中的文件夾和文件?

public class Compress { 
private static final int BUFFER = 2048; 

private String[] _files; 
private String _zipFile; 

public Compress(String[] files, String zipFile) { 
_files = files; 
_zipFile = zipFile; 
} 

public void zip() { 
try { 
    BufferedInputStream origin = null; 
    FileOutputStream dest = new FileOutputStream(_zipFile); 

    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); 

    byte data[] = new byte[BUFFER]; 

    for(int i=0; i < _files.length; i++) { 
    Log.v("Compress", "Adding: " + _files[i]); 
    FileInputStream fi = new FileInputStream(_files[i]); 
    origin = new BufferedInputStream(fi, BUFFER); 
    ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1)); 
    out.putNextEntry(entry); 
    int count; 
    while ((count = origin.read(data, 0, BUFFER)) != -1) { 
     out.write(data, 0, count); 
    } 
    origin.close(); 
    } 

    out.close(); 
} catch(Exception e) { 
    e.printStackTrace(); 
} 

} 

} 

我打電話這個類以這種方式在另一個類:

String[] files = {mainFolderPath+"/text1.txt", mainFolderPath+ "/text2.txt", mainFolderPath +"/NewFolder"}; 
     Compress compress = new Compress(files, sourceFile.getAbsolutePath()); 
     compress.zip(); 

在運行我得到一個IOException應用。

你能告訴我如何壓縮包含另一個文本文件以及文本文件「text1.txt」和「text2.txt」的「NewFolder」?

謝謝。

+0

什麼行引發異常?你有沒有做過讀/寫文件的測試?也許你只是沒有權限訪問文件系統? – enTropy

+1

謝謝你的回覆。如果我在String [] files數組中包含「NewFolder」,則這是引發IOException的行。如果我不添加「NewFolder」,我可以得到僅包含「text1.txt」和「text2.txt」的壓縮文件夾。 **((count = origin.read(data,0,BUFFER))!= -1)** –

回答

0

我得到了我的問題,即解決方案;只壓縮包含文件夾和某些文本文件的特定文件夾的內容。這是代碼:

public class Compress { 

private static final int BUFFER = 2048; 
private String[] _files; 
private String _zipFile; 

public Compress(String[] files, String zipFile) { 

      _files = files; 
      _zipFile = zipFile; 
} 

public void zip() { 


    try { 
     BufferedInputStream origin = null; 
     FileOutputStream dest = new FileOutputStream(_zipFile); 

     ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
       dest)); 

     byte data[] = new byte[BUFFER]; 

     for (int i = 0; i < _files.length; i++) { 
      Log.v("Compress", "Adding: " + _files[i]); 
      String name = _files[i]; 
      File f = new File(_files[i]); 
      if (f.isDirectory()) { 
       name = name.endsWith("/") ? name : name + "/"; 

       for (String file : f.list()) { 
        System.out.println(" checking " + file); 
        System.out 
          .println("The folder name is: " + f.getName()); 
        out.putNextEntry(new ZipEntry(f.getName() + "/" + file)); 
        FileInputStream fi = new FileInputStream(_files[i] 
          + "/" + file); 
        origin = new BufferedInputStream(fi, BUFFER); 
        int count; 
        while ((count = origin.read(data, 0, BUFFER)) != -1) { 
         out.write(data, 0, count); 
        } 
        origin.close(); 
       } 
       System.out.println(" checking folder" + name); 
      } else { 
       FileInputStream fi = new FileInputStream(_files[i]); 
       origin = new BufferedInputStream(fi, BUFFER); 
       ZipEntry entry = new ZipEntry(_files[i].substring(_files[i] 
         .lastIndexOf("/") + 1)); 
       out.putNextEntry(entry); 
       int count; 
       while ((count = origin.read(data, 0, BUFFER)) != -1) { 
        out.write(data, 0, count); 
       } 
       origin.close(); 
      } 
     } 

     out.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

} 
0

它適用於我!!!!!!!!在我所有的項目,我需要....

public String[] _files; 
    public String _zipFile = "/mnt/sdcard/123.zip"; 



    /////zIIIiiiiPPPPPer 

public class Compress { 
private static final int BUFFER = 2048; 
public Compress(String[] files, String zipFile) { 
    _files = files; 
    _zipFile = zipFile; 
} 
public void zip() { 
    try { 
    BufferedInputStream origin = null; 
    FileOutputStream dest = new FileOutputStream(_zipFile); 
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); 
    String path = "/mnt/sdcard/out"; 
    File fileDir = new File(path); 
    if(!fileDir.exists() || !fileDir.isDirectory()) 
    { 
     return; 
    } 
    String[] _files = fileDir.list(); 
    for (int i = 0 ; i < _files.length ; i++ ) 
    { 
      _files[i] = path + "/"+ _files[i]; 
    } 
    byte data[] = new byte[BUFFER]; 
    for(int i=0; i < _files.length; i++) { 
     Log.v("Compress", "Adding: " + _files[i]); 
     FileInputStream fi = new FileInputStream(_files[i]); 
     origin = new BufferedInputStream(fi, BUFFER); 
     ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1)); 
     out.putNextEntry(entry); 
     int count; 
     while ((count = origin.read(data, 0, BUFFER)) != -1) { 
     out.write(data, 0, count); 
     } 
     origin.close(); 
    } 
    out.close(); 
    } catch(Exception e) { 
    e.printStackTrace(); 
    } 

} 

要調用它只是做像水木清華...

Compress cs = new Compress(_files, _zipFile); 
+0

謝謝您的回覆。我用上面的代碼,但它仍然給我同樣的例外,即; java.io.IOException:遇到「NewFolder」之後的目錄。 –

+0

你有清單中的嗎? – timonvlad

+0

如果是 - 給出你的錯誤日誌? – timonvlad

相關問題