使用Android创建ZIP文件

3

如何从XML文件创建ZIP文件?

我想要备份所有收件箱中的邮件,将其存储为XML文件,并压缩该XML文件后存储在SD卡上。

5个回答

7
以下代码解决了我的问题。
public class makeZip {
    static final int BUFFER = 2048;

    ZipOutputStream out;
    byte data[];

    public makeZip(String name) {
        FileOutputStream dest=null;
        try {
            dest = new FileOutputStream(name);
        } 
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out = new ZipOutputStream(new BufferedOutputStream(dest));
        data = new byte[BUFFER];
    }

    public void addZipFile (String name) {
        Log.v("addFile", "Adding: ");
        FileInputStream fi=null;
        try {
            fi = new FileInputStream(name);
            Log.v("addFile", "Adding: ");
        }
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.v("atch", "Adding: ");
        }
        BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry entry = new ZipEntry(name);
        try {
            out.putNextEntry(entry);
            Log.v("put", "Adding: ");
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int count;
        try {
            while((count = origin.read(data, 0, BUFFER)) != -1) {
               out.write(data, 0, count);
               //Log.v("Write", "Adding: "+origin.read(data, 0, BUFFER));
            }
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
             Log.v("catch", "Adding: ");
        }
        try {
            origin.close();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void closeZip () {
        try {
            out.close();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

2

如果您在SD卡中有一个文件夹,想要创建一个zip压缩包,那么只需将此代码复制并粘贴到您的项目中,它将为您提供一个zip文件夹。此代码将创建一个仅包含文件而没有嵌套文件夹的文件夹的zip压缩包。您可以进一步进行修改。

 String []s=new String[2]; //declare an array for storing the files i.e the path of your source files
  s[0]="/mnt/sdcard/Wallpaper/pic.jpg";    //Type the path of the files in here
  s[1]="/mnt/sdcard/Wallpaper/Final.pdf"; // path of the second file

  zip((s,"/mnt/sdcard/MyZipFolder.zip");    //call the zip function


 public void zip(String[] files, String zipFile) 
 { 
    private String[] _files= files;
    private String _zipFile= zipFile;  

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.d("add:",_files[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(); 
} 

同时,使用以下代码在android-manifest.xml中添加权限:

  <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

1
使用以下代码,但如果您想保存到SD卡,请使用FileOutputStream而不是ByteArrayOutputStream。
private String compressData(String uncompressedData) {
    String compressedData = null;
    try {
        if (uncompressedData.length() > 200) {
            // Perform Compression.
            byte[] originalBytes = uncompressedData.getBytes();

            Deflater deflater = new Deflater();
            deflater.setInput(originalBytes);
            deflater.finish();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[8192];
            while (!deflater.finished()) {
                int byteCount = deflater.deflate(buf);
                baos.write(buf, 0, byteCount);
            }
            deflater.end();

            byte[] compressedBytes = baos.toByteArray();

            compressedData = new String(compressedBytes, 0, compressedBytes.length);
        }
    }
    catch (Exception e) {
        compressedData = null;
    }
    return compressedData;
}


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接