1

我能够压缩包含在特定文件夹中的文件。这是我使用的代码:

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”?

谢谢你。

4

2 回答 2

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(); 
   } 

 }   

称之为Just do smth like ...

Compress cs = new Compress(_files, _zipFile);
于 2012-09-24T11:38:47.890 回答
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();
    }
}

}
于 2012-09-28T06:09:06.230 回答