I am trying to create a zip file and upload that zip file in AWS S3 via a multipart upload route. However, I am facing an issue where the file is successfully getting generated however, the contents inside the files are getting corrupted.
Here is the error that I am getting when I am trying to unarchive the file :
Archive: test2MB.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of test2MB.zip or
test2MB.zip.zip, and cannot find test2MB.zip.ZIP, period.
Here are the code snippets that I have written for this
private Single<List<CompletedPart>> zipAndUploadZipParts(String uploadId) {
List<Single<CompletedPart>> uploadPartObservables = new ArrayList<>();
final int[] partNumber = {Constants.START_PART_NUMBER};
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
return zipMetadata(zipOutputStream)
.andThen(uploadZipPart(byteArrayOutputStream, uploadPartObservables, partNumber, uploadId)
.onErrorResumeNext(error -> {
this.contextRx.log().error(String.format("placeholder", partNumber[0], this.logIdentifier, error.getMessage()), error);
return Completable.error(new RuntimeException("Placeholder"));
}))
.andThen(finalizeZipUpload(uploadPartObservables, zipOutputStream, byteArrayOutputStream));
}
private Completable zipMetadata(ZipOutputStream zipOutputStream) {
return Completable.create(emitter -> {
try {
ZipEntry metadataEntry = new ZipEntry(Constants.METADATA_FILE_NAME);
zipOutputStream.putNextEntry(metadataEntry);
zipOutputStream.write(this.catalogObjectsMetadata.getBytes());
zipOutputStream.closeEntry();
emitter.onComplete();
} catch (IOException exception) {
this.contextRx.log().error(String.format(Constants.LOG_ERROR_ZIPPING_METADATA, this.logIdentifier, exception.getMessage()), exception);
emitter.onError(new RuntimeException(String.format(Constants.ERROR_ZIPPING_METADATA)));
}
});
}
private Completable uploadZipPart(ByteArrayOutputStream byteArrayOutputStream, List<Single<CompletedPart>> uploadPartObservables, int[] partNumber, String uploadId) {
return Completable.create(emitter -> {
try {
byte[] zipData = byteArrayOutputStream.toByteArray();
uploadPartObservables.add(uploadPart(zipData, partNumber[0]++, uploadId));
byteArrayOutputStream.reset();
this.contextRx.log().info(String.format("Uploaded part %d for uploadId %s", partNumber[0] - 1, uploadId));
emitter.onComplete();
} catch (Exception exception) {
emitter.onError(exception);
}
});
}
private Single<List<CompletedPart>> finalizeZipUpload(List<Single<CompletedPart>> uploadPartObservables, ZipOutputStream zipOutputStream, ByteArrayOutputStream byteArrayOutputStream) {
return Single.defer(() -> {
try {
zipOutputStream.finish();
} catch (IOException e) {
return Single.error(new RuntimeException("Error finishing zip output stream", e));
} finally {
try {
zipOutputStream.close();
} catch (IOException e) {
return Single.error(new RuntimeException("Error closing zip output stream", e));
}
try {
byteArrayOutputStream.close();
} catch (IOException e) {
return Single.error(new RuntimeException("Error closing byte array output stream", e));
}
}
return Single.merge(uploadPartObservables)
.toList()
.doOnSuccess(parts -> this.contextRx.log().info(String.format(Constants.ALL_PARTS_UPLOADED, this.logIdentifier, parts)))
.onErrorReturnItem(new ArrayList<>());
});
}
Can someone guide what is wrong with my code which is corrupting my zip file?
Contents inside the zip file shouldn’t get corrupted.