I'm struggling with extracting a zip file contents using the evil Storage Access Framework, as you may know; I can't use any File objects so I have to use ZipInputStream, ZipOutputStream and DocumentFile, Here's the zip file structure:
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
Here's my code:
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);
}
}
}
And here's how the out put look like:
Thanks
Use
Unzip();
method form FileUtilsPlus library