Assignments are not expressions, and only expressions are allowed in this context - Error when convert Java to Kotlin

Nick Bapu

I have well managed code and project in Java. But I need to develop another project from it in Kotlin. So, I converted all code as possible in Kotlin. But there is code of ZipFileManager.kt which is use to zip/unzip files.

Here is code(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))
        }

    }
}

So, I am trying through this code but It's shows compile time error like:

Assignments are not expressions, and only expressions are allowed in this context in the fun zip at the line while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1)

and give another same compile time error at line while ((ze = zin.nextEntry) != null) and at the line while ((read = zin.read(buffer)) != -1).

So, my big problem is to use this code in Kotlin. So, any body can help who have knowledge of Kotlin and how can I use this type of looping structure in Kotlin?

There is also I have Java code if any body want to see:

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

I also try to manage loop like:

do {
    ze = zin.nextEntry
} while (ze != null)

But than file is not got unzip properly or its broken. So, if any body have idea of managing this type of loop than it will very helpful.

Nikunj Paradva

I am converting your Java code to Kotlin

I have facing this issue earlier Assignments are not expressions, and only expressions are allowed in this context

Use this code here is your solution

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Assignments are not expressions and only expressions are allowed in this context - Kotlin

Assignments are not expressions - Kotlin

"Subqueries are not allowed in this context. Only scalar expressions are allowed."

Thymeleaf: Only variable expressions returning numbers or booleans are allowed in this context

Only variable expressions returning numbers or booleans are allowed in this context

Kotlin - Type of `if` and `when` Expressions

org.thymeleaf.exceptions.TemplateProcessingException: Only variable expressions returning numbers or booleans are allowed in this context

Powershell - "Expressions are only allowed as the first element of a pipeline"

Only SubQuery expressions that are top level conjuncts are allowed

When are fold expressions as template parameters allowed?

What to do when error 'Ordering by expressions of type STRUCT is not allowed' and ordering is in qualify row number over() statement?

compiler error when using inline lambda expressions in java

regular expressions: $ not allowed

Simple Example: Expressions are only allowed as the first element of a pipeline

Basic PowerShell Script Issue: "Expressions are only allowed as the first element of a pipeline"

Basic PowerShell Script Issue: “Expressions are only allowed as the first element of a pipeline”

How to solve this error - ORA-22818: subquery expressions not allowed?

Regular Expressions - Semicolon passing unit test when not allowed character

Java expressions

LL1 grammar for arithmetic expressions and assignments

assignments to unpacked array must be aggregate expressions

Kotlin - print function expressions

Lambda expressions in Kotlin

Java Regular Expressions Not Compiling - No Match Available Error

Understanding "lambda expressions" in Java in the context of "lambda" and "anonymous classes"

Is it possible to convert a string to an int[] using only lambda expressions?

Error: "Only the Kotlin standard library is allowed to use the 'kotlin' package"

Unexpected tokens (use ';' to separate expressions on the same line) error in Kotlin

How to convert to lambda expressions