【问题标题】:Creating a ZIP file with Android使用 Android 创建 ZIP 文件
【发布时间】:2014-05-09 14:19:45
【问题描述】:

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

我想以 XML 格式备份我的所有收件箱消息,然后压缩 XML 文件并将其存储在 SD card 中。

【问题讨论】:

    标签: android sms compression zip backup


    【解决方案1】:

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

    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】:

      如果您在 SDCard 中有一个文件夹,并且您想创建一个 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" />
      

      【讨论】:

        【解决方案3】:

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            【解决方案5】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-10-17
              • 1970-01-01
              • 2019-04-02
              • 2013-05-30
              • 1970-01-01
              • 1970-01-01
              • 2013-02-23
              相关资源
              最近更新 更多