7-Zip-JBinding-4Android
7-Zip-JBinding-4Android copied to clipboard
openOutArchiveZip error
i download you project as module of my app, but it is run error. here is my log:
2021-06-28 16:05:31.407 31522-31522/me.zhanghai.android.files A/i.android.file: java_vm_ext.cc:570] JNI DETECTED ERROR IN APPLICATION: JNI GetObjectClass called with pending exception java.lang.ClassNotFoundException: Didn't find class "net.sf.sevenzipjbinding.junit.jbindingtools.Callback1" on path: DexPathList[[zip file "/data/app/me.zhanghai.android.files-m6S3hIFb-vFHBLlvPO81pw==/base.apk"],nativeLibraryDirectories=[/data/app/me.zhanghai.android.files-m6S3hIFb-vFHBLlvPO81pw==/lib/arm64, /data/app/me.zhanghai.android.files-m6S3hIFb-vFHBLlvPO81pw==/base.apk!/lib/arm64-v8a, /system/lib64, /system/product/lib64]]
and my code :
override fun archive3(fileName: String, level: Int) {
val TAG = "123"
val path = viewModel.currentPath.resolve(fileName).toString()
var success = false
var raf: RandomAccessFile? = null
var outArchive: IOutCreateArchiveZip? = null
try {
raf = RandomAccessFile(path, "rw")
outArchive = SevenZip.openOutArchiveZip()
outArchive.setLevel(level)
outArchive.createArchive(RandomAccessFileOutStream(raf),1,object :IOutCreateCallback<IOutItemZip>{
override fun setTotal(total: Long) {
Log.i(TAG,"1:"+total.toString())
}
override fun setCompleted(complete: Long) {
Log.i(TAG,"2:"+complete.toString())
}
override fun setOperationResult(operationResultOk: Boolean) {
Log.i(TAG,"3:"+operationResultOk.toString())
}
override fun getStream(index: Int): ISequentialInStream? {
return null
}
override fun getItemInformation(
index: Int,
outItemFactory: OutItemFactory<IOutItemZip>?
): IOutItemZip? {
return null
}
})
} catch (e: SevenZipException) {
Log.i(TAG, e.message.toString())
} catch (e: Exception) {
Log.i(TAG, e.message.toString())
} finally {
if (outArchive != null) {
try {
outArchive.close()
} catch (e: IOException) {
Log.i(TAG, e.message.toString())
success = false
}
}
if (raf != null) {
try {
raf.close()
} catch (e: IOException) {
Log.i(TAG, e.message.toString())
success = false
}
}
}
if (success) {
Log.i(TAG, "Compression operation succeeded")
}
}
I created the following project and included your sample code: https://github.com/omicronapps/Archive3
This works fine after adding just a couple of lines of code that were missing in your sample code.
Also see here for instructions on how to create a Zip archive: http://sevenzipjbind.sourceforge.net/compression_snippets.html#create-specific-api-zip
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
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
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
archive3("test.zip", 1);
}
fun archive3(fileName: String, level: Int) {
val TAG = "123"
val path = File(filesDir, fileName);//viewModel.currentPath.resolve(fileName).toString()
var success = true
var raf: RandomAccessFile? = null
var outArchive: IOutCreateArchiveZip? = null
try {
raf = RandomAccessFile(path, "rw")
outArchive = SevenZip.openOutArchiveZip()
outArchive.setLevel(level)
outArchive.createArchive(RandomAccessFileOutStream(raf),1,object :IOutCreateCallback<IOutItemZip>{
override fun setTotal(total: Long) {
Log.i(TAG,"1:"+total.toString())
}
override fun setCompleted(complete: Long) {
Log.i(TAG,"2:"+complete.toString())
}
override fun setOperationResult(operationResultOk: Boolean) {
Log.i(TAG,"3:"+operationResultOk.toString())
}
override fun getStream(index: Int): ISequentialInStream? {
val content = byteArrayOf(0, 1, 2, 3);
return ByteArrayStream(content, true)
}
override fun getItemInformation(
index: Int,
outItemFactory: OutItemFactory<IOutItemZip>?
): IOutItemZip? {
val item = outItemFactory!!.createOutItem()
item.dataSize = 4;
item.propertyPath = "path";
return item
}
})
} catch (e: SevenZipException) {
Log.i(TAG, e.message.toString())
success = false
} catch (e: Exception) {
Log.i(TAG, e.message.toString())
success = false
} finally {
if (outArchive != null) {
try {
outArchive.close()
} catch (e: IOException) {
Log.i(TAG, e.message.toString())
success = false
}
}
if (raf != null) {
try {
raf.close()
} catch (e: IOException) {
Log.i(TAG, e.message.toString())
success = false
}
}
}
if (success) {
Log.i(TAG, "Compression operation succeeded")
}
}
}
i have solve this problem with same solution, thank for your help. have working on it for 4 weeks. may be i can add some example in readme.
Sure. Please feel free to open a pull request with changes to README.md.
@VoidK2 Can you please help me with https://github.com/omicronapps/7-Zip-JBinding-4Android/issues/22
Find the reason why ZIP format cannot be compressed,the return parameter type of IOutCreateCallback
is ISequentialInStream
,but in jni level ,is running as random access。 so it need ISeekableStream
。 we need IInStream
in this callback when compress zip archive, not ISequentialInStream
.