Как заархивировать папку и подпапку с помощью zip4j и outputstream

В программе, которую я пишу, я беру uri местоположения, чтобы сохранить zip-файл от пользователя. Затем я пытаюсь заархивировать файлы и папки, используя библиотеку zip4j и outpustream в Android. Я изменил код в этом ответе Stackoverflow и использовал zip4j вместо этого. Мой измененный код создает zip-файл, однако он поврежден.

это мой код, написанный на Котлине:

class ZipBuilder {
    private fun buildZipParameters(compressionMethod: CompressionMethod, compressionLevel: CompressionLevel,
                                   encrypt: Boolean,
                                   encryptionMethod: EncryptionMethod?,
                                   aesKeyStrength: AesKeyStrength?
    ): ZipParameters? {
        val zipParameters = ZipParameters()
        zipParameters.compressionMethod = compressionMethod
        zipParameters.compressionLevel = compressionLevel
        return zipParameters
    }


    fun zipFileAtPath(sourcePath: String?, toLocation: ParcelFileDescriptor?): Boolean {
        println("zipFileAtPath is called")
        val BUFFER = 2048
        val sourceFile = File(sourcePath!!)
        val zipParameters = buildZipParameters(CompressionMethod.DEFLATE, CompressionLevel.NORMAL,
            false, null, null)

        try {
            var origin: BufferedInputStream? = null
            val desc = toLocation
            val dest = FileOutputStream(desc!!.fileDescriptor)
            val out = ZipOutputStream(BufferedOutputStream(dest)) 

            if (sourceFile.isDirectory) {
                zipParameters.rootFolderNameInZip = sourcePath
                zipSubFolder(out, sourceFile, sourceFile.parent!!.length, zipParameters!!)
            } else {
                val data = ByteArray(BUFFER)
                val fi = FileInputStream(sourcePath)
                origin = BufferedInputStream(fi, BUFFER)

                zipParameters!!.fileNameInZip = getLastPathComponent(sourcePath)
                zipParameters.lastModifiedFileTime = sourceFile.lastModified()

                out.putNextEntry(zipParameters)

                var count: Int = 0
                while (fi.read(data).also({ count = it }) != -1) {
                    out.write(data, 0, count)
                }
            }
            out.close()
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
            return false
        }
        return true
    }

    @Throws(IOException::class)
    private fun zipSubFolder(
        out: ZipOutputStream, folder: File, basePathLength: Int, zipParameters: ZipParameters
    ) {
        val BUFFER = 2048
        val fileList = folder.listFiles()
        var origin: BufferedInputStream
        fileList?.forEach { file ->
            if (file.isDirectory) {
                zipSubFolder(out, file, basePathLength, zipParameters)
            } else {
                val data = ByteArray(BUFFER)
                val unmodifiedFilePath = file.path
                val relativePath = unmodifiedFilePath
                    .substring(basePathLength)
                val fi = FileInputStream(unmodifiedFilePath)
                origin = BufferedInputStream(fi, BUFFER)


                zipParameters.fileNameInZip = relativePath
                zipParameters.lastModifiedFileTime = file.lastModified()
                out.putNextEntry(zipParameters)


                var count: Int = 0

                while (fi.read(data).also({ count = it }) != -1) {
                    out.write(data, 0, count)
                }
                origin.close()
            }
        }
    }

    fun getLastPathComponent(filePath: String): String? {
        val segments = filePath.split("/").toTypedArray()
        return if (segments.size == 0) "" else segments[segments.size - 1]
    }

}

Буду признателен, если кто-нибудь подскажет, в чем может быть проблема.


person Corey    schedule 06.05.2020    source источник


Ответы (1)