Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6770865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:21:52+00:00 2026-05-26T15:21:52+00:00

I have a big zipfile that I need to split in multiple zip files.

  • 0

I have a big zipfile that I need to split in multiple zip files. In the method I’m now creating I have a List object.

This is the code I have got:

 //All files have the same basefilename/
 string basefilename = Path.GetFileNameWithoutExtension(entries[0].FileName);
 MemoryStream memstream = new MemoryStream();
 ZipFile zip = new ZipFile();
 foreach (var entry in entries)
 {
    string newFileName = basefilename + Path.GetExtension(entry.FileName);
    zip.AddEntry(newFileName, entry.OpenReader());
 }

 zip.Save(memstream);

 //this will later go in an file-io handler class.
 FileStream outstream = File.OpenWrite(@"c:\files\"+basefilename+ ".zip");
 memstream.WriteTo(outstream);
 outstream.Flush();
 outstream.Close();

And this is the error I get at the save() call :

{Ionic.Zlib.ZlibException: Bad state (invalid block type) at Ionic.Zlib.InflateManager.Inflate(FlushType flush) at
Ionic.Zlib.ZlibCodec.Inflate(FlushType flush) at
Ionic.Zlib.ZlibBaseStream.Read(Byte[] buffer, Int32 offset, Int32
count) at Ionic.Zlib.DeflateStream.Read(Byte[] buffer, Int32 offset,
Int32 count) at Ionic.Crc.CrcCalculatorStream.Read(Byte[] buffer,
Int32 offset, Int32 count) at
Ionic.Zip.SharedUtilities.ReadWithRetry(Stream s, Byte[] buffer, Int32
offset, Int32 count, String FileName) at
Ionic.Zip.ZipEntry._WriteEntryData(Stream s) at
Ionic.Zip.ZipEntry.Write(Stream s) at Ionic.Zip.ZipFile.Save() at
Ionic.Zip.ZipFile.Save(Stream outputStream) at

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:21:53+00:00Added an answer on May 26, 2026 at 3:21 pm

    here’s what you’re doing wrong: You have multiple pending calls to ZipEntry.OpenReader() in a single ZipFile instance. You can have at most, only one pending ZipEntry.OpenReader().

    Here’s why: There is only one Stream object created when you instantiate a given zip file with ZipFile.Read() or new ZipFile(), passing the name of an existing file. When you call ZipEntry.OpenReader() , it results in a Seek() in the Stream object, to move the file pointer to the beginning of the compressed bytestream for that particular entry. When you call ZipEntry.OpenReader() again, it results in another Seek() to a different location in the stream. So by adding entries and calling OpenReader() in succession, you are calling Seek() repeatedly, but only the last one will be valid. The stream cursor will be placed at the start of the data for the entry corresponding to the last call to ZipEntry.OpenReader().

    To fix it: Scrap your approach. The simplest way to create a new zipfile with fewer entries than an existing zip file is this: instantiate a ZipFile by reading the existing file, then remove the entries you don’t want, then call ZipFile.Save() to a new path.

    using (var zip = ZipFile.Read("c:\\dir\\path\\to\\existing\\zipfile.zip")) 
    {
        foreach (var name in namesToRemove) // IEnumerable<String>
        {
           zip[name].Remove();
        }
        zip.Save("c:\\path\\to\\new\\Archive.zip");
    } 
    

    EDIT
    What this does at the time you call Save(): the library reads the raw, compressed data for the entries you have NOT removed from the filesystem file, and writes them into a new archive file. This is really fast because it does not decompress and recompress each entry in order to put it into the new, smaller zip file. Basically it reads slices of binary data out of the original zip file, and concatenates them together to form the new, smaller zip file.

    To produce multiple smaller files, you can do this repeatedly with the original zip file; just wrap the above in a loop and vary the files you remove, and the filename of the new, smaller archive. Reading an existing zipfile is also pretty fast.


    As an alternative, you could decompress and extract each entry, and then recompress and write the entry into a new zip file. That is the long way around, but it is possible. In that case, for each smaller zipfile you want to create, you will need to create two ZipFile instances. Open the first one by reading the original zip archive. For each entry you want to keep, create a MemoryStream, extract content from an entry into that MemoryStream, and remember to call Seek() in the mem stream to reset the cursor on the memory stream. Then using the second ZipFile instance, call AddEntry(), using that MemoryStream as the source for the added entry. Call ZipFile.Save() only on the second instance.

    using (var orig = ZipFile.Read("C:\\whatever\\OriginalArchive.zip"))
    {
        using (var smaller = new ZipFile())
        {
          foreach (var name in entriesToKeep) 
          { 
             var ms = new MemoryStream();
             orig[name].Extract(ms); // extract into stream
             ms.Seek(0,SeekOrigin.Begin);
             smaller.AddEntry(name,ms);
          }
          smaller.Save("C:\\location\\of\\SmallerZip.zip");
        }   
    }
    

    This works, but it involves decompression and recompression of each entry that goes into the smaller zip, which is inefficient and unnecessary.


    If you don’t mind the inefficiency of the decompression and recompression, there’s an alternative you can employ: call the ZipFile.AddEntry() overload that accepts opener and closer delegates. What this does is defer the call to OpenReader() til the time the entry is being written to the new, smaller zip file. The effect is that you have just one pending OpenReader() at a time.

    using(ZipFile original = ZipFile.Read("C:\\path.to\\original\\Archive.zip"),
          smaller = new ZipFile())
    {
        foreach (var name in entriesToKeep)
        {
            zip.AddEntry(zipEntryName,
                         (name) => original[name].OpenReader(),
                         null);
        }
    
        smaller.Save("C:\\path.to\\smaller\\Archive.zip");
    }
    

    It’s still inefficient, because each entry gets decompressed and recompressed, but it’s a little less inefficient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have big system that make my system crash hard. When I boot up,
I have big URL list, which I have to download in parallel and check
I have big element at the top of the webpage that sides down with
How would you maintain the legacy applications that: Has no unit tests have big
Given that I have a zip file called archive.zip that contains a file called
I have BIG table with multiple indexes in postgres. It has indexes on db_timestamp,
I'm new to Jira and have big problem with creating tables inside my testing
I have big content. get by file_get_content. It like this: <div class=nwsdetail> <div class=othernws>
i have big decision tree with many Yes/No questions. i need implement some code
The Android documentation being the mess that it is, I have big problem understanding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.