Sign in to follow this  
Lenolian

[.net] [C#] zip file

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");
file.CopyTo(archive);
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
Fs.PHYSFS_init("");

// 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
Fs.PHYSFS_close(imageFile);

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this