如何将整个文件夹压缩成android中的单个文件

时间:2015-03-25 20:17:37

标签: android

我目前正在使用下面的代码压缩文件夹,但遗憾的是我无法压缩缓存文件夹,如果我尝试这样做我只创建一个虚拟zip文件... 但它与位于内部和外部存储器中的任何其他文件夹一起工作正常。 我使用权限来编写和读取内部文件,但我还是面临这个奇怪的问题

public void zip(){

     String p = "/data/data/com.example.****/cache/";
     Date now = new Date();
     File dir = new File (p,"up"); 
        if (!dir.exists())
        {
            dir.mkdirs();
        }
      String fileName = formatter.format(now);
     String _zipFile  =p +"up/"+fileName+".zip"; 
   try  { 
     BufferedInputStream origin = null; 
     FileOutputStream dest = new FileOutputStream(_zipFile); 
     ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); 
    File fileDir = new File("/data/data/com.example.****/cache/****/");
                 String[] _files = fileDir.list(); 
     for ( int i = 0 ; i < _files.length ; i++  )
     { 
           _files[i] = fileDir + "/"+ _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(); 
   } }

0 个答案:

没有答案