0

How do I insert a text file into an existing zip file where I have a stream of both the files.

Sample code is below:

I am using IOutils.copy(in, out), but it is replacing the content. I need to insert the file and keep the existing content.

public URI createACopyOfFile(String requestId) throws Exception {
    BlobOutputStream blobOutputStream = null;
    BlobInputStream blobInputStream = null;
    try {
        long startTime = System.currentTimeMillis();
        System.out.print("Start createACopyOfFile ->" + startTime);
        long endTime = 0;
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
        CloudBlobClient cloudBlobClient = storageAccount.createCloudBlobClient();
        CloudBlobContainer container = cloudBlobClient.getContainerReference("tme-container");
        container.createIfNotExists();
        String blobName = "dummy.zip";
        CloudBlockBlob sourceBlob = container.getBlockBlobReference(blobName);
        CloudBlockBlob targetBlob = container.getBlockBlobReference(requestId + "/dummy_copy.zip");
        targetBlob.startCopyFromBlob(sourceBlob);

        blobOutputStream = targetBlob.openOutputStream();
        CloudBlockBlob blob = container.getBlockBlobReference(requestId + "/" + "activation.txt");
        blob.uploadText("Hello world");

        blobInputStream = blob.openInputStream();

        IOUtils.copy(blobInputStream, blobOutputStream);

        endTime = System.currentTimeMillis();
        System.out.print("End createACopyOfFile -> " + endTime);
        System.out.println("Total time taken for createACopyOfFile - > " + (endTime - startTime) / 1000);
        return targetBlob.getUri();
    } finally {
        try {
            if (blobInputStream != null)
                blobInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (blobOutputStream != null)
                blobOutputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Satish Bhuria
  • 81
  • 2
  • 13
  • Please edit your question to be more specific. Is this a Java-specific issue? Or is there something related to Azure (and if yes to Azure, how so? The question is unclear). – David Makogon Sep 13 '16 at 19:03
  • yes , it is related to java only but file are stored on azure platform. I am using blob storage for storing a large file and on runtime i have to insert a file into a large file which is there on azure. – Satish Bhuria Sep 13 '16 at 19:24
  • Sample code i have attached . Please check – Satish Bhuria Sep 13 '16 at 19:25
  • when i did IOUtils.copy(blobInputStream, blobOutputStream); all the content on inputstream is copied and replacing the existing content – Satish Bhuria Sep 13 '16 at 19:26

1 Answers1

0

I want to tell you that there is no ways to do it by azure SDK. When you add file to the zip file, the file will reorganize its content and generate a new zip file. There is one method that you can download the .zip file and append file in local by JAVA. After then, you can upload your file to azure storage again. But your file is a big file, so it's not a feasible method.

The references: Appending files to a zip file with Java

Hope it helps. Any concerns, please feel free to let me know.

Community
  • 1
  • 1
johnny
  • 319
  • 1
  • 7