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.
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.
Comments