gpt4 book ai didi

java - ZipOutputStream 在 Android 上生成损坏的 zip 文件

转载 作者:太空狗 更新时间:2023-10-29 12:41:08 24 4
gpt4 key购买 nike

我已经使用 zip 存档从应用程序备份用户数据,我将数据库和共享首选项文件复制到 zip 存档并计算输入文件的 MD5 校验和以防止用户修改备份数据。

为了从存档中恢复,我将备份文件解压缩到临时目录,检查校验和,然后将首选项\数据库文件复制到相应的文件夹中。

我的一些用户提示该应用生成损坏的备份文件(zip 文件确实已损坏)。

这是将所有文件压缩为 zip 文件的代码:

public void backup(String filename) {
File file = new File(getBackupDirectory(), filename);
FileOutputStream fileOutputStream = null;
ZipOutputStream stream = null;
try {
String settingsMD5 = null;
String databaseMD5 = null;
if (file.exists())
file.delete();
fileOutputStream = new FileOutputStream(file);
stream = new ZipOutputStream(new BufferedOutputStream(fileOutputStream));
File database = getDatabasePath(databaseFileName);
File dataDirectory = getFilesDir();
if (dataDirectory != null) {
File settings = new File(dataDirectory.getParentFile(), "/shared_prefs/" + PREFERENCES_FILENAME);
settingsMD5 = zipFile("preferences", stream, settings);
}
databaseMD5 = zipFile("database.db", stream, database);

JSONObject jsonObject = new JSONObject();
try {
jsonObject.put(META_DATE, new SimpleDateFormat(DATE_FORMAT, Locale.US).format(new Date()));
jsonObject.put(META_DATABASE, databaseMD5);
jsonObject.put(META_SHARED_PREFS, settingsMD5);
} catch (Exception e) {
e.printStackTrace();
}

InputStream metadata = new ByteArrayInputStream(jsonObject.toString().getBytes("UTF-8"));
zipInputStream(stream, metadata, new ZipEntry("metadata"));
stream.finish();
stream.close();
stream = null;
return file;
} catch (FileNotFoundException e) {
//handling errrors
} catch (IOException e) {
//handling errrors
}
}

private String zipFile(String name, ZipOutputStream zipStream, File file) throws FileNotFoundException, IOException {
ZipEntry zipEntry = new ZipEntry(name);
return zipInputStream(zipStream, new FileInputStream(file), zipEntry);
}

private String zipInputStream(ZipOutputStream zipStream, InputStream fileInputStream, ZipEntry zipEntry) throws IOException {
InputStream inputStream = new BufferedInputStream(fileInputStream);
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
if (messageDigest != null)
inputStream = new DigestInputStream(inputStream, messageDigest);
} catch (NoSuchAlgorithmException e) {
}

zipStream.putNextEntry(zipEntry);
inputToOutput(inputStream, zipStream);
zipStream.closeEntry();
inputStream.close();

if (messageDigest != null) {
return getDigestString(messageDigest.digest());
}
return null;
}

private String getDigestString(byte[] digest) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < digest.length; i++) {
String hex = Integer.toHexString(0xFF & digest[i]);
if (hex.length() == 1) {
hex = new StringBuilder("0").append(hex).toString();
}
hexString.append(hex);
}
return hexString.toString();
}

private void inputToOutput(InputStream inputStream, OutputStream outputStream) throws IOException {
byte[] buffer = new byte[BUFFER];
int count = 0;
while ((count = inputStream.read(buffer, 0, BUFFER)) != -1) {
outputStream.write(buffer, 0, count);
}
}

最佳答案

您可以考虑使用 zip4j 库。我通过使用这个库解决了我遇到的问题(同样的问题 - 不同的方向)。一些 zip 文件无法使用原生 android 实现解码,但可以使用 zip4j。您也可以通过使用 zip4j 进行压缩来解决您的问题。

关于java - ZipOutputStream 在 Android 上生成损坏的 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25594792/

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