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

前端之家收集整理的这篇文章主要介绍了android-分配不是表达式,并且在此上下文中仅允许表达式-将Java转换为Kotlin时出错 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在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))
    }
  }
}
原文链接:https://www.f2er.com/android/531411.html

猜你在找的Android相关文章