android拆分zip文件并制作文件

时间:2013-08-21 07:35:44

标签: java android file zip

我想从服务器下载几个zip文件部分(file1.z01,file1.z02,file1.z03等)

现在下载完所有部件后,我们可以通过代码将这些部分合并成一个zip并制作一个zip文件吗?

3 个答案:

答案 0 :(得分:0)

你可以使用它只是相应地更改值...

import android.util.Log; 
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipOutputStream; 


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

  } 

}

答案 1 :(得分:0)

请点击此链接,您可能会收到此消息 download files

Convert to zip

答案 2 :(得分:0)

Zip4j有一种方便的方法来合并拆分的zip文件。

ZipFile.mergeSplitFiles(File outputZipFile)

会将所有拆分的zip文件合并到一个用outputZipFile

指定的zip文件中

让我们说你已经拆分了zip文件:ZipFileName.z01,ZipFileName.z02,ZipFileName.z03 ..... ZipFileName.zip下面的代码将它们合并到MergedZipFile.zip

import java.io.File;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;

public class MergeZipFile {

    public static void main(String[] args) {
        try {
            ZipFile zipFile = new ZipFile("ZipFileName.zip");
            zipFile.mergeSplitFiles(new File("MergedZipFile.zip"));
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }

}