Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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))
    }
  }
}

大佬总结

以上是大佬教程为你收集整理的android-分配不是表达式,并且在此上下文中仅允许表达式-将Java转换为Kotlin时出错 全部内容,希望文章能够帮你解决android-分配不是表达式,并且在此上下文中仅允许表达式-将Java转换为Kotlin时出错 所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。