Open mipastgt opened 2 months ago
Further investigations revealed that the test only works if shallow = true
and the source directory also does not contain any sub-directories and isZip64 = false. In all other cases the test fails. Zipping a directory recursively is thus not possible.
The code has several problems which I tried to fix in file ZipFile.kt in the common code.
With these changes the code works now for me on the Java VM and if I set isZip64
to false.
On iOS I am getting strange memory deallocation errors and the resulting ZIP file is corrupt.
Maybe you make something from this input.
I did not create a pull request because the whole thing still does not work and I have also not tested the other platforms.
suspend fun zipDirectory(directory: File,
shallow: Boolean = false,
withParent: Boolean = false,
filter: ((pathName: String) -> Boolean)? = null)
override suspend fun zipDirectory(
directory: File,
shallow: Boolean,
withParent: Boolean,
filter: ((pathName: String) -> Boolean)?
) {
if (!directory.isDirectory)
throw IllegalArgumentException("Path ${file.file.path} is not a directory")
val directoryPath = if (withParent) {
directory.fullPath.dropLast(directory.name.length + 1)
} else {
directory.fullPath
}
val parentPath = if (directoryPath.endsWith(File.pathSeparator))
directoryPath
else
directoryPath + File.pathSeparator
zipOneDirectory(directory, shallow, parentPath, filter)
}
private suspend fun zipOneDirectory(directory: File,
shallow: Boolean,
parentPath: String,
filter: ((pathName: String) -> Boolean)?)
{
directory.listFiles.forEach { path ->
val name = (if (path.isDirectory && !path.fullPath.endsWith(File.pathSeparator))
path.fullPath + File.pathSeparator
else
path.fullPath).replace(parentPath, "")
if (filter?.invoke(name) != false) {
if (path.isDirectory) {
if (!shallow) zipOneDirectory(path, shallow, parentPath, filter)
} else {
zipFile(path, path.fullPath.replace(parentPath, "").replace('\\','/'))
}
}
}
}
The following test should create a ZIP file of a directory but it just fails with an internal error instead.
The error is
I have tested this on a Mac and the test fails for desktop, android and iOS.