使用 Android 创建 ZIP 文件

发布于 2025-01-05 00:31:23 字数 152 浏览 0 评论 0原文

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

我想以 XML 格式备份所有收件箱消息,并压缩 XML 文件并将其存储在 SD 上卡

How can I create a ZIP file from an XML file?

I want to take a backup of all my inbox messages in XML, and compress the XML file and store it on an SD card.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

叫思念不要吵 2025-01-12 00:31:23

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

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

The following code solved my problem.

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();
        }
    }
鹿! 2025-01-12 00:31:23

如果您在 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" />

If you have a FOLDER in SDCard and you want to create a zip of it, then simply copy and paste this code in your project and it will give you a zip Folder. This code will create a zip of the folder which only contains files no nested folder should be inside. You can further modify for your self.

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

}

Also add permissions in android-manifest.xml using this code

  <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
烧了回忆取暖 2025-01-12 00:31:23

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

The Android SDK has the gZip API, by which you can read/write conpressed data. See public class GZIPOutputStream.

等风来 2025-01-12 00:31:23

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

Use the following code, but if you want to save to the SD card, do a fileoutputstream rather than 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文