分配不是表达式,并且在此上下文中仅允许表达式-将Java转换为Kotlin时出错

时间:2019-06-07 06:15:43

标签: android kotlin

我在Java中拥有管理良好的代码和项目。但是我需要在Kotlin中从中开发另一个项目。因此,我尽可能在Kotlin中转换了所有代码。但是有ZipFileManager.kt的代码可用于zip / unzip文件。

这是代码(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))
        }

    }
}

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

Assignments are not expressions, and only expressions are allowed in this contextfun 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)

但是文件没有正确解压缩或损坏。因此,如果有人有管理这种循环的想法,那将非常有帮助。

1 个答案:

答案 0 :(得分:2)

我正在将您的Java代码转换为Kotlin

我之前遇到过这个问题 Assignments are not expressions, and only expressions are allowed in this context

在这里使用此代码是您的解决方案

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))
    }
  }
}