Search code examples
azurefile-uploadazure-blob-storageazure-sdk-for-java

Azure Storage zip file corrupted after upload (using Azure Storage Blob client library for Java)


problem: zip file with csv files generated from data seems to be corrupted after upload to Azure Blob Storage.

zip file before upload looks like this:

enter image description here

and everything works fine. That same zip file after upload is corrupted and looks like this: enter image description here

During upload I use Azure Storage Blob client library for Java (v. 12.7.0, but I tried also previous versions). This is code I use (similar to example provided in SDK readme file):

public void uploadFileFromPath(String pathToFile, String blobName) {
     BlobClient blobClient = blobContainerClient.getBlobClient(blobName);
     blobClient.uploadFromFile(pathToFile);
}

And I get uploaded file:

enter image description here

When I download file directly from storage explorer, file is already corrupted. What I'm doing wrong?


Solution

  • Eventually it was my fault. I didn't close ZipOutputStream before uploading file. It is not much of a problem when you use try with resources and just want to generate local file. But in my case I want to upload file to Blob Storage (still in the try section). File was incomplete (not closed) so it appeared on storage with corrupted data. This is what I should do from the very beginning.

    private void addZipEntryAndDeleteTempCsvFile(String pathToFile, ZipOutputStream zipOut,
            File file) throws IOException {
        LOGGER.info("Adding zip entry: {}", pathToFile);
        zipOut.putNextEntry(new ZipEntry(pathToFile));
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] bytes = new byte[1024];
            int length;
            while ((length = fis.read(bytes)) >= 0) {
                zipOut.write(bytes, 0, length);
            }
            zipOut.closeEntry();
            file.delete()
        }
        zipOut.close(); // missing part
    }
    

    After all, thank you for your help @JimXu. I really appreciate that.