android - Assignments are not expressions, and only expressions are allowed in this context - 将 Java 转换为 Kotlin 时出错

标签 android kotlin

我在 Java 中管理良好的代码和项目。但我需要用它在 Kotlin 中开发另一个项目。所以,我在 Kotlin 中尽可能地转换了所有代码。但是有 ZipFileManager.kt 的代码,用于压缩/解压缩文件。

这是代码(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
                    while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1)     {
                        out.write(data, 0, 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, 0, read)
                            }
                            zin.closeEntry()
                        }
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
            Log.e("UnZipException", Log.getStackTraceString(e))
        }

    }
}

所以,我正在尝试通过这段代码,但它显示编译时错误,如:

赋值不是表达式,在此上下文中只允许使用表达式 fun zip 中的行 while ((count = origin.read(data , 0, BUFFER_SIZE)) != -1)

并在 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, 0, BUFFER_SIZE)) != -1) {
                        out.write(data, 0, 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, 0, 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, 0, BUFFER_SIZE);
                while (count != -1) {
                    out.write(data, 0, count)
                    count = origin.read(data, 0, 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, 0, read)
                            read = zin.read(buffer)
                        }
                        zin.closeEntry()
                    }
                }
                ze = zin.nextEntry
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
        Log.e("UnZipException", Log.getStackTraceString(e))
    }
  }
}

关于android - Assignments are not expressions, and only expressions are allowed in this context - 将 Java 转换为 Kotlin 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56488871/

相关文章:

android.database.sqlite.SQLiteException : near "Foreign": syntax error (code 1):

android - C 中的 Sum native 方法 - java.lang.UnsatisfiedLinkError : Native method not found

c# - 如何在 Xamarin forms Android 中设置 PageTiltle FontFamily 和 Size

Android:AbsListView.setItemChecked() 导致 ActionMode 销毁并重新创建?

kotlin - 如何将 Kotlin 扩展放入类文件中?

无法重新分配 Android val

android - 无法确定任务 ':app:lintVitalRelease' 的依赖关系

android - 使用RxJava,Retrofit,RxKotlin平面图依次调用多个API

java - getLifecycle().getState() 在 onStart() 之后仍然是 CREATED

android - 如何从 Kotlin 中的 Hashmap 获取值的键值?