使用Android创建ZIP文件

时间:2012-02-10 09:40:41

标签: android sms compression zip backup

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

我希望以XML格式备份所有收件箱邮件,并压缩XML文件并将其存储在SD card上。

5 个答案:

答案 0 :(得分:4)

以下代码解决了我的问题。

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

答案 1 :(得分:0)

答案 2 :(得分:0)

Android SDK具有gZip API,您可以通过该API读取/写入压缩数据。请参阅 public class GZIPOutputStream

答案 3 :(得分:0)

使用以下代码,但如果要保存到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;
}

答案 4 :(得分:0)

如果你在SDCard中有一个FOLDER而你想创建它的拉链,那么只需将这个代码复制并粘贴到你的项目中它就会给你一个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" />