android - 赋值不是表达式,在此上下文中只允许使用表达式 - 将 Java 转换为 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))
}
}
}
所以,我正在尝试通过这段代码,但它显示编译时错误,如:
Assignments are not expressions, and only expressions are allowed in this context
fun zip
在线上while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1)
while ((ze = zin.nextEntry) != null)
并在 line和 at给出另一个相同的编译时错误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
我之前遇到过这个问题
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))
}
}
}
推荐阅读
- reactjs - 如何从反应应用程序调试反应库
- hyperledger-fabric - 是否可以在我的主机上获取文件以供 Hyperledger Fabric 中的链代码使用?
- java - 如何检查 BitmapFactory.decodeStream() 是否已完全下载图像?
- css - 显示垂直滚动但允许浮动溢出的容器
- firebase - 无法在 Firebase 云功能中部署 PubSub 功能
- linux - 除了历史记录之外,有没有办法找到我们在 linux 中运行的命令
- context-free-grammar - 为最多两个 0 的偶数长度字构造 CFG
- flutter - 无法使用传播集合
- java - 香农熵不打印任何东西
- c++ - 在 QT 中运行一个有延迟的函数