Zip attachment in Corda How to read zip file using InputStream

0 votes

I uploaded a zip file using the attachment option in Corda and I got a SecureHash as output. Then I tried to download the same zip file using this hash as input to openAttachment method I got a InputStream. I tried to read the contents of this InputStream using BufferReader but I  could not because it is encrypted.  I got the "import java.util.zip.ZipEntry" package to read the zip file content but didn’t work. How can I read the contents of the zip file?

The code is as follows:

fun main(args: Array<String>) :String {
    require(args.isNotEmpty()) { "Usage: uploadBlacklist <node address>" }
    args.forEach { arg ->
        val nodeAddress = parse(args[0])
        val rpcConnection = CordaRPCClient(nodeAddress).start("user1", "test")
        val proxy = rpcConnection.proxy
         val attachmentInputStream = File(args[1]).inputStream()
        val attachmentHash = proxy.uploadAttachment(attachmentInputStream)
        print("AtachmentHash"+attachmentHash)
        // Download the attachment
        val inputString = proxy.openAttachment(attachmentHash).bufferedReader().use { it.readText() }
        println("The contents are ")
        print(inputString)
        val file = File("OutputFile.txt")
        file.writeText(inputString)
        rpcConnection.notifyServerAndClose()
    }
    return("File downloaded successfully in the path")
}
Jul 25, 2018 in Blockchain by slayer
• 29,350 points
1,134 views

1 answer to this question.

0 votes

You can not directly read the content using the InputStream. To read the contents you first need to convert the InputStream to JarInputStream (It extends the class java.util.zip.ZipInputStream and is used to read contents of JAR file). Then using the JarInputStream method, find an entry and read the contents.

The code to do it is as follows:

val attachmentDownloadInputStream = proxy.openAttachment(attachmentHash)
val attachmentJar = JarInputStream(attachmentDownloadInputStream)


while (attachmentJar.nextEntry.name != expectedFileName) {
    attachmentJar.nextEntry
}


val contents = attachmentJar.bufferedReader().readLines()

You can use the following to download the attachment:

// downloading the attachment
        val attachmentDownloadInputStream = proxy.openAttachment(attachmentHash)
        val attachmentJar = JarInputStream(attachmentDownloadInputStream)
        var contents =""
        //Reading the contents
        while(attachmentJar.nextJarEntry!=null){
            contents = contents + attachmentJar.bufferedReader().readLine()

Have a look at an example here: https://www.corda.net/develop/samples.html

answered Jul 25, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer
0 votes
1 answer

How to get all address and send ethers in solidity using a loop?

I found a similar code somewhere: contract  Holders{ uint ...READ MORE

answered Jul 31, 2018 in Blockchain by digger
• 26,740 points
2,551 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to read file from subprocess.open in python?

you call: out, err = p.communicate() READ MORE

answered Aug 24, 2018 in Blockchain by digger
• 26,740 points
1,221 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP