我在Java中管理良好的代码和项目.但是我需要在Kotlin中开发另一个项目.因此,我在Kotlin中转换了所有代码.但是有ZipfileManager.kt的代码,用于zip / unzip文件.
这是代码(科特琳):
object ZipfileManager { private val BUFFER_SIZE = 6 * 1024 @Throws(IOException::class) fun zip(files: Array<String>,zipfile: String) { var origin: BufferedinputStream? = null val out = ZipOutputStream(bufferedoutputstream(fileOutputStream(zipfile))) try { val data = ByteArray(BUFFER_SIZE) for (file in files) { val fi = fileinputStream(file) origin = BufferedinputStream(fi,BUFFER_SIZE) try { val entry = ZipEntry(file.substring(file.lastIndexOf("/") + 1)) out.putNextEntry(entry) var count: Int while ((count = origin.read(data,BUFFER_SIZE)) != -1) { out.write(data,count) } } finally { origin.close() } } } finally { out.close() } } fun unzip(zipfileUrl: String,fileLocation: String) { try { val f = file(fileLocation) if (!f.isDirectory) { f.mkdirs() } ZipinputStream(fileinputStream(zipfileUrl)).use { zin -> var ze: ZipEntry? = null while ((ze = zin.nextEntry) != null) { // Log.e("UnZipfile","UnzipPing...."); val path = fileLocation + ze!!.name if (ze.isDirectory) { val unzipfile = file(path) if (!unzipfile.isDirectory) { unzipfile.mkdirs() } } else { fileOutputStream(path,false).use { fout -> val buffer = ByteArray(1024) var read: Int while ((read = zin.read(buffer)) != -1) { fout.write(buffer,read) } zin.closeEntry() } } } } } catch (e: Exception) { e.printstacktrace() Log.e("UnZipException",Log.getStackTraceString(e)) } }}
因此,我正在尝试这段代码,但是它显示了编译时错误,例如:
赋值不是表达式,在此上下文中,仅当((count = origin.read(data,BUFFER_SIZE))!= -1)处的乐趣zip中才允许使用表达式.
并在while((ze = zin.nextEntry)!= null)行和while((read = zin.read(buffer))!= -1)行给出另一个相同的编译时错误.
因此,我最大的问题是在Kotlin中使用此代码.那么,任何人都可以帮助了解Kotlin的人,如何在Kotlin中使用这种类型的循环结构?
如果有人想看的话,我也有Java代码:
public class ZipfileManager { private static int BUFFER_SIZE = 6 * 1024; public static voID zip(String[] files,String zipfile) throws IOException { BufferedinputStream origin = null; ZipOutputStream out = new ZipOutputStream(new bufferedoutputstream(new fileOutputStream(zipfile))); try { byte data[] = new byte[BUFFER_SIZE]; for (String file : files) { fileinputStream fi = new fileinputStream(file); origin = new BufferedinputStream(fi,BUFFER_SIZE); try { ZipEntry entry = new ZipEntry(file.substring(file.lastIndexOf("/") + 1)); out.putNextEntry(entry); int count; while ((count = origin.read(data,BUFFER_SIZE)) != -1) { out.write(data,count); } } finally { origin.close(); } } } finally { out.close(); } } public static voID unzip(String zipfileUrl,String fileLocation) { try { file f = new file(fileLocation); if (!f.isDirectory()) { f.mkdirs(); } try (ZipinputStream zin = new ZipinputStream(new fileinputStream(zipfileUrl))) { ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) {// Log.e("UnZipfile","UnzipPing...."); String path = fileLocation + ze.getname(); if (ze.isDirectory()) { file unzipfile = new file(path); if (!unzipfile.isDirectory()) { unzipfile.mkdirs(); } } else { try (fileOutputStream fout = new fileOutputStream(path,false)) { byte[] buffer = new byte[1024]; int read; while ((read = zin.read(buffer)) != -1) { fout.write(buffer,read); } zin.closeEntry(); } } } } } catch (Exception e) { e.printstacktrace(); Log.e("UnZipException",Log.getStackTraceString(e)); } }}
我也尝试管理像这样的循环:
do { ze = zin.nextEntry} while (ze != null)
但是,文件无法正确解压缩或损坏.因此,如果有人有管理这种循环的想法,那将非常有帮助.最佳答案我正在将您的Java代码转换为Kotlin
我之前遇到过这个问题
分配不是表达式,并且在此上下文中仅允许表达式
使用此代码是您的解决方案
object ZipfileManager {private val BUFFER_SIZE = 6 * 1024@Throws(IOException::class)fun zip(files: Array<String>,zipfile: String) { var origin: BufferedinputStream? = null val out = ZipOutputStream(bufferedoutputstream(fileOutputStream(zipfile))) try { val data = ByteArray(BUFFER_SIZE) for (file in files) { val fi = fileinputStream(file) origin = BufferedinputStream(fi,BUFFER_SIZE) try { val entry = ZipEntry(file.substring(file.lastIndexOf("/") + 1)) out.putNextEntry(entry) var count: Int= origin.read(data,BUFFER_SIZE); while (count != -1) { out.write(data,count) count = origin.read(data,BUFFER_SIZE) } } finally { origin.close() } } } finally { out.close() }}fun unzip(zipfileUrl: String,fileLocation: String) { try { val f = file(fileLocation) if (!f.isDirectory) { f.mkdirs() } ZipinputStream(fileinputStream(zipfileUrl)).use { zin -> var ze: ZipEntry? = null ze = zin.nextEntry while (ze != null) { // Log.e("UnZipfile","UnzipPing...."); val path = fileLocation + ze!!.name if (ze.isDirectory) { val unzipfile = file(path) if (!unzipfile.isDirectory) { unzipfile.mkdirs() } } else { fileOutputStream(path,false).use { fout -> val buffer = ByteArray(1024) var read: Int= zin.read(buffer) while (read != -1) { fout.write(buffer,read) read = zin.read(buffer) } zin.closeEntry() } } ze = zin.nextEntry } } } catch (e: Exception) { e.printstacktrace() Log.e("UnZipException",Log.getStackTraceString(e)) } }}
总结 以上是内存溢出为你收集整理的android-分配不是表达式,并且在此上下文中仅允许表达式-将Java转换为Kotlin时出错 全部内容,希望文章能够帮你解决android-分配不是表达式,并且在此上下文中仅允许表达式-将Java转换为Kotlin时出错 所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)