Closed tien1812 closed 3 years ago
I created the following project and included your sample code: https://github.com/omicronapps/compressFile
I modified this to compress some byte arrays into a Zip archive and this works fine. Can you modify this source code sample to reproduce the issue? Then please be sure to create a pull request with the changes.
Also see here for instructions on how to create an archive with any format supported by 7-Zip: http://sevenzipjbind.sourceforge.net/compression_snippets.html#create-generic
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import net.sf.sevenzipjbinding.*
import net.sf.sevenzipjbinding.impl.OutItemFactory
import net.sf.sevenzipjbinding.impl.RandomAccessFileOutStream
import net.sf.sevenzipjbinding.util.ByteArrayStream
import java.io.File
import java.io.IOException
import java.io.RandomAccessFile
import java.util.*
class MainActivity : AppCompatActivity() {
val TAGS = "compressFile"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val itemCount = 3
val outputPath = File.createTempFile("temp", ".zip", applicationContext.cacheDir).absolutePath
val archiveFormat = ArchiveFormat.ZIP
val callback = MyCreateCallback()
compressFile(itemCount, outputPath, archiveFormat, callback)
}
fun setTotalProgress(total: Long) {Log.d(TAGS, "setTotalProgress: " + total)}
fun updateProgress(complete: Long) {Log.d(TAGS, "updateProgress: " + complete)}
fun onSuccess() {Log.d(TAGS, "onSuccess")}
val paths = arrayOf("file1", "file2", "file3")
open inner class MyCreateCallback : IOutCreateCallback<IOutItemAllFormats> {
private var fileComplete = 0
override fun setTotal(total: Long) {
setTotalProgress(total)
}
override fun setCompleted(complete: Long) {
updateProgress(complete)
}
override fun setOperationResult(operationResultOk: Boolean) {
fileComplete++
if (fileComplete == paths!!.size) {
onSuccess()
}
}
override fun getItemInformation(
index: Int,
outItemFactory: OutItemFactory<IOutItemAllFormats>?
): IOutItemAllFormats {
var attr = PropID.AttributesBitMask.FILE_ATTRIBUTE_UNIX_EXTENSION
val path = paths!![index]
val file = File(path)
val item = outItemFactory!!.createOutItem()
if (file.isDirectory) {
item.propertyIsDir = true
attr = attr or PropID.AttributesBitMask.FILE_ATTRIBUTE_DIRECTORY
attr = attr or (0x81ED shl 16)
} else {
// item.dataSize = file.length()
item.dataSize = 4;
attr = attr or (0x81a4 shl 16)
}
item.propertyPath = path
item.propertyAttributes = attr
item.propertyLastModificationTime = Date(file.lastModified())
return item
}
override fun getStream(index: Int): ISequentialInStream? {
// val path = paths!![index]
// val file = File(path)
// if (file.isDirectory) return null
// return ByteArrayStream(file.readBytes(), true)
val content = byteArrayOf(0, 1, 2, 3);
return ByteArrayStream(content, true)
}
}
fun compressFile(
itemCount: Int,
outputPath: String,
archiveFormat: ArchiveFormat,
callback: IOutCreateCallback<IOutItemAllFormats>
) {
val path = File(outputPath)
var success: Boolean
var raf: RandomAccessFile? = null
var outArchive: IOutCreateArchive<IOutItemAllFormats>? = null
try {
raf = RandomAccessFile(path, "rw")
outArchive = SevenZip.openOutArchive(archiveFormat)
if (outArchive is IOutFeatureSetLevel) {
outArchive.setLevel(5)
}
if (outArchive is IOutFeatureSetMultithreading) {
outArchive.setThreadCount(2)
}
outArchive.createArchive(
RandomAccessFileOutStream(raf),
itemCount,
callback
)
success = true
} catch (e: SevenZipException) {
Log.i(TAGS, e.message.toString())
success = false
} catch (e: Exception) {
Log.i(TAGS, e.message.toString())
success = false
} finally {
if (outArchive != null) {
try {
outArchive.close()
} catch (e: IOException) {
Log.i(TAGS, e.message.toString())
success = false
}
}
if (raf != null) {
try {
raf.close()
} catch (e: IOException) {
Log.i(TAGS, e.message.toString())
success = false
}
}
}
if (success) {
Log.i(TAGS, "Compression operation succeeded")
}
}
}
yes , i did it . Thank you very much !
this is my code , when i compress a dir , updateProgress stops at 38% and dir has no files .
` open inner class MyCreateCallback : IOutCreateCallback {
private var fileComplete = 0
override fun setTotal(total: Long) {
setTotalProgress(total)
}
`
` fun compressFile( itemCount: Int, outputPath: String, archiveFormat: ArchiveFormat, callback: IOutCreateCallback
) {
val path = File(outputPath)
var success: Boolean
var raf: RandomAccessFile? = null
var outArchive: IOutCreateArchive? = null