如何将文本文件插入到现有的zip文件中,其中包含两个文件的流。
示例代码如下:
我正在使用IOutils.copy(in, out)
,但它正在替换内容。我需要插入文件并保留现有内容。
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();
}
}
}
答案 0 :(得分:0)
我想告诉你,azure SDK没有办法做到这一点。将文件添加到zip文件时,该文件将 重新组织其内容并生成新的zip文件。 有一种方法可以下载.zip文件并通过JAVA在本地附加文件。之后,您可以再次将文件上传到azure存储。但是你的文件是一个大文件,所以这不是一个可行的方法。
参考文献: Appending files to a zip file with Java
希望它有所帮助。如有任何疑虑,请随时告诉我。