0
votes

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?

2
Please try to download file with code blobClient.downloadToFile.Jim Xu
The file is already corrupted in storage. There's no difference in downloading it by java app or through Storage Explorer on Azure portal. In both cases zip file is corrupted.Mac Karczewski
If you directly upload the file via portal, is it ok for you?Jim Xu
@JimXu Yes. When I upload file via portal everything works fine. But content type is different: "application/x-zip-compressed" instead of "application/octet-stream".Mac Karczewski

2 Answers

1
votes

According to your description, I suggest you use the following method to upload you zip file

public void uploadFromFile(String filePath, ParallelTransferOptions parallelTransferOptions, BlobHttpHeaders headers, Map<String,String> metadata, AccessTier tier, BlobRequestConditions requestConditions, Duration timeout)

We can use the method to set content type

For example

BlobHttpHeaders headers = new BlobHttpHeaders()
     .setContentType("application/x-zip-compressed");
 Integer blockSize = 4* 1024 * 1024; // 4MB;
 ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions(blockSize, null, null);
blobClient.uploadFromFile(pathToFile,parallelTransferOptions,headers,null, AccessTier.HOT, null, null);

For more details, please refer to the document

0
votes

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.