Skip to content

Add files to existing zip inside a blob storage [Stream is not seekable] #700

Closed

Description

@sergiojrdotnet

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'.

Version of SharpZipLib

  • 1.3.3
  • .NET 6

Obtained from

  • Package installed using NuGet

Activity

  1. 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
  2. piksel commented on Jan 19, 2022

    @piksel
    Member

    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).

  3. locked and limited conversation to collaborators on Jan 19, 2022
  4. converted this issue into a discussion #701 on Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions