You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add files to existing zip inside a blob storage [Stream is not seekable] #700
I'm trying to create an azure function to receive a list of file urls and add them directly to a single zip file stored into a blob storage.
private static async Task Compress(string sourceFileName, string sourceFileUrl)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
CloudBlobClient outputClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer outputContainer = outputClient.GetContainerReference("zip-container");
CloudAppendBlob outputBlob = outputContainer.GetAppendBlobReference("Test.zip");
using var httpClient = new HttpClient();
Stream sourceStream = await httpClient.GetStreamAsync(sourceFileUrl);
if (!await outputBlob.ExistsAsync())
{
await outputBlob.CreateOrReplaceAsync();
using var zipStream = new ZipOutputStream(await outputBlob.OpenWriteAsync(false));
zipStream.SetLevel(3);
ZipEntry zipEntry = new(sourceFileName);
zipEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(zipEntry);
StreamUtils.Copy(sourceStream, zipStream, new byte[4096]);
zipStream.CloseEntry();
}
else
{
ZipFile zipFile = new(await outputBlob.OpenWriteAsync(false)); //Exception: Stream is not seekable
zipFile.BeginUpdate();
CustomStaticDataSource sds = new();
sds.SetStream(sourceStream);
zipFile.Add(sds, sourceFileName);
zipFile.CommitUpdate();
zipFile.Close();
}
}
The problem occurs when adding more files to existing zip blob. When I try to initialize the ZipFile class with the stream it throws the exception 'Stream is not seekable'.
changed the title [-]Add files to existing zip file inside a blob storage [Stream is not seekable][/-][+]Add files to existing zip inside a blob storage [Stream is not seekable][/+]on Jan 19, 2022
Yeah, to update a zip file, you need to make changes to already written data, hence the seeking. The way the zip format works it's not possible to do what you are trying to do. Either you have to store the blob inside a buffer (MemoryStream), update it, and then write the new zipfile to the blob, or use another format (like tar).
Activity
[-]Add files to existing zip file inside a blob storage [Stream is not seekable][/-][+]Add files to existing zip inside a blob storage [Stream is not seekable][/+]piksel commentedon Jan 19, 2022
Yeah, to update a zip file, you need to make changes to already written data, hence the seeking. The way the zip format works it's not possible to do what you are trying to do. Either you have to store the blob inside a buffer (
MemoryStream), update it, and then write the new zipfile to the blob, or use another format (like tar).