gpt4 book ai didi

android - 使用 Android 创建 ZIP 文件

转载 作者:行者123 更新时间:2023-11-29 00:41:12 26 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于android - 使用 Android 创建 ZIP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9225673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com