Jump to content
  • Advertisement
Sign in to follow this  

[.net] [C#] zip file

This topic is 4621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First let me explain what i am trying to achieve. I am coding a layer over .net filesystem so i could use folders, compressed files or any other sort of archives, the same way. Exemple which copy a file found in a zip file and copy it to a folder :
ArchiveManager manager = new ArchiveManager();
ArchiveInfo archive = manager.GetArchive("L:\Test2");
FileInfo file = manager.GetFile(@"L:\Test\Source.zip\Source\Code.zip\Test.txt");
So right now i am using SharpZipLib to deal with the zip files, but this libary doesnt allow editing and modifying of an exising zip file. The workaround i have now is to build another compressed file and to copy the data from the previous one, which is not elegant or performant. I am looking for another lib that could do that, but got no luck yet and i dont really want to code one myself if i can avoid it. So if you know one that could that, it would be perfect. Thanks.

Share this post

Link to post
Share on other sites
The source is available for SharpZipLib. You could modify it to support "add in place" compression, however, I think it should be noted that just about every commercial compression package I've ever used has added a file to an existing archive by creating a new one and then moving it into place. (Improves compression results, recalculates all of the inner tables, etc.)

If your primary problem is speed and the archives are generally small you could work with a MemoryStream object instead of a FileStream. Makes it lightning fast (until you need to write it to disk :)).

Share this post

Link to post
Share on other sites
You could implement the functionality of PhysicsFS through Tao.PhysFs.

PhysFS was inspired by the Quake file systems in which you have multiple zip/pak files, but can load a single file from any one of those zip files without having to specify the source. The way it works is that you add a "search paths" where your data files reside. These search paths can be sub-folders, zip files, etc. Once you setup your search paths, you can load any file within any of the search paths without having to deal with whether it's being loaded from a different folder, a zip file, a Doom WAD file, etc.

The output from loading in the file can be pretty much anything. A byte array, a stream, a string, etc. Here's an SDL.NET example of it being used to load an image from a zip file.

// Initiate PhysFS

// Allow PhysFS to look in data.zip for files
Fs.PHYSFS_addToSearchPath("data.zip", 1);

// Open surface from zip
IntPtr imageFile = Fs.PHYSFS_openRead("sdldotnet_full.png");

// Read it into a byte array
byte[] imageBytes;
Fs.PHYSFS_read(imageFile, out imageBytes, 1, (uint)Fs.PHYSFS_fileLength(imageFile));

// Create a surface from the loaded bytes.
Surface surf = new Surface(imageBytes);

// close the file

Share this post

Link to post
Share on other sites
Yes, this is what i would like in the end. Actually i had no problem when it was just about reading from an archive. The problem arised when i wanted to be able to add or update contents in a zip. I thought that a zip could be modified, but it seems its not allowed.

So i end up doing like talonius suggested, using a memory stream to the modification and write back the zip file.

Thanks for the replies.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!