Here's the scenario.
User uploads a zip file from a form. On the backend, I get the ZipInputStream and convert the inputstream to bytes and upload to GCS
`public String upload(
String bucketName,
String objectName,
String contentType,
InputStream objectInputStream)
throws IOException {
if (contentType == null) contentType = ContentType.CONTENT_TYPE_TEXT_PLAIN_UTF8;
BlobId blobId;
if (largeFile) {
blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(contentType).build();
WriteChannel writer = storage.writer(blobInfo);
if (storage.get(blobId) != null) writer = storage.update(blobInfo).writer();
byte[] buffer = new byte[1024];
int limit;
while ((limit = objectInputStream.read(buffer)) >= 0) {
try {
writer.write(ByteBuffer.wrap(buffer, 0, limit));
} catch (Exception e) {
logger.error("Exception uploadObject", e);
}
}
writer.close();
} else {
byte[] objectBytes = ByteStreams.toByteArray(objectInputStream);
blobId = storeByteArray(storage, bucketName, objectName, contentType, objectBytes);
if (Objects.isNull(blobId)) return null;
}
return url(bucketName, objectName);
}`
COde that gets the filepart and calls the above method
ZipInputStream filePartInputStream = new ZipInputStream(filePart.getInputStream());
storageGateway.uploadObject(
"bucket_name",
"objectname",
filePart.getContentType(),
filePartInputStream
);
The upload works as expected but when I download the zip folder from GCS bucket, it seems to be corrupted. I was not able to unzip it.
Am I missing anyhting here ? If not what's the correct way to upload a zip file to google cloud storage
file downloaded_file? - Mike SchwartzZipInputStream, just pass thefilePart.getInputStreamto thestorageGateway. I don't remember theZipInputStreamAPI, but it is possible that your current solution is actually unpackingzipon-the-fly and you're writing not-a-zip file to the storage. - xSAVIKxcontent-encodingfor zip files isapplication/zipand notgzip. - John Hanleygsutil cp, and get a complete zip file like you originally uploaded? That would tell you the upload worked successfully and it was your download path that's having problems. - Mike Schwartz