如您所知,我正在努力使用邪恶的存储访问框架提取 zip 文件内容;我不能使用任何 File 对象,所以我必须使用 ZipInputStream、ZipOutputStream 和 DocumentFile,这是 zip 文件结构:
Folder 1/ABC 001.jpg
Folder 1/ABC 002.jpg
Folder 2/ABC 003.jpg
Folder 2/ABC 004.jpg
Folder 2/Folder 3/ABC 005.jpg
Folder 2/Folder 3/ABC 006.jpg
Folder 2/Folder 3/Folder 4/ABC 007.jpg
Folder 2/Folder 3/Folder 4/ABC 008.jpg
ABC 009.jpg
这是我的代码:
public void extractZipFile(DocumentFile srcZipFile, DocumentFile destDir) throws IOException
{
ZipEntry entry;
InputStream inputStream = resolver.openInputStream(srcZipFile.getUri());
try (java.util.zip.ZipInputStream zipInputStream = new java.util.zip.ZipInputStream(inputStream))
{
while ((entry = zipInputStream.getNextEntry()) != null)
{
DocumentFile currentDestDir = destDir;
if (!entry.isDirectory())
{
unzipFile(entry, zipInputStream, currentDestDir);
}
else
{
String finalFolderName = entry.getName().replace("/", "");
currentDestDir = destDir.createDirectory(finalFolderName);
}
}
}
inputStream.close();
}
private void unzipFile(ZipEntry fileEntry, java.util.zip.ZipInputStream zipInputStream, DocumentFile destDir) throws IOException
{
int readLen;
byte[] readBuffer = new byte[BUFFER_SIZE];
DocumentFile destFile = destDir.createFile("*/*", fileEntry.getName());
try (OutputStream outputStream = resolver.openOutputStream(destFile.getUri()))
{
while ((readLen = zipInputStream.read(readBuffer)) != -1)
{
outputStream.write(readBuffer, 0, readLen);
}
}
}
下面是输出的样子:
谢谢