0
如何將文本文件插入到現有的zip文件中,並在其中存在兩個文件的流。如何將文本文件附加到java中的現有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();
}
}
}
請編輯您的問題,以更具體。這是一個Java特有的問題嗎?或者是否有與Azure相關的東西(如果是的話,Azure是如此,這個問題還不清楚)。 –
是的,它只與java相關,但文件存儲在azure平臺上。 我使用blob存儲來存儲一個大文件,並且在運行時我必須將一個文件插入一個大文件,這個文件在azure上。 –
我已附上示例代碼。當我做了IOUtils.copy(blobInputStream,blobOutputStream)時,請檢查 –