0
votes

I tried uploading zip files using uploadAttachemnt method where I got a secureHash as output. I tried to download the same attachment using the hash as input to openAttachmnet method I got a InputStream. When I tried to read the contents of the inputStream using BuffeReader it is encrypted. I realised I had to unzip the file and read it so I got this package "import java.util.zip.ZipEntry" to read a zip file content I am unsure whether I can read the zip file contents using InputStream. How should I read the zip file contents using the InputStream? If not should I unzip and upload the file ?

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

2 Answers

2
votes

You need to convert the InputStream returned by openAttachment into a JarInputStream. You can then use the JarInputStream's methods to locate an entry and read its contents:

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

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

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

For an example, take a look at the RPC client code of the Blacklist sample CorDapp here: https://github.com/corda/samples.

1
votes

I had many files inside the zip file. so I tried this code and it worked fine. Thanks for your input joel.

// 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()

        }
        println("The contents are $contents")