0

Is it possible to create a file within a Zip file?

Let's say I have a file abc.zip and want to create a file in it named SomeFile.txt. Is it possible? [I am using a dll Ionic.Zip which helps with Zip file manipulation]. Also, I want to do this without unzipping it.

Jon
  • 428,835
  • 81
  • 738
  • 806
Ebad Masood
  • 2,389
  • 28
  • 46
  • 2
    uncompress abc.zip and then add somefile.txt and then compress the combind file – santosh singh May 11 '12 at 07:29
  • 3
    @Geek, he just said he doesn't want to unzip the archive........... – walther May 11 '12 at 07:29
  • Referring to wikipedia it should be possible to add a new file without unzipping thw entire archive. But I have no knowledge how to do programmatically. MAby you find some algorithms if you look in OpenSource programms, that are able to create/ work with zip. – Shegit Brahm May 11 '12 at 07:37
  • 2walther, to my mind that any zip application works in same way that was described by geek. Just hide this from end user. It is encoding task. I am not sure that it is trivial task to insert coded data in certain place of other coded data without decoding target data. – RredCat May 11 '12 at 07:39
  • @RredCat, https://en.wikipedia.org/wiki/Solid_compression – sschrass May 11 '12 at 08:03
  • 2SatelliteSD - it is cool. But I see from this explanation that later version of 7zip use "solid block" which was created from files with same extension. Can we say in this case that add file without unpacking other? And we told about Ionic.Zip not 7zip. – RredCat May 11 '12 at 08:43

2 Answers2

5

Sure. For example, if you have already created SomeFile.txt:

using (var zip = ZipFile.Read("abc.zip"))
{
    zip.AddFile("SomeFile.txt");
    zip.Save();
}
Jon
  • 428,835
  • 81
  • 738
  • 806
  • he doesn't want to create a new file, he want to inject SomeFile.txt into abc.zip. – sschrass May 11 '12 at 07:36
  • @SatelliteSD: What do you mean? Are you asking how many bits are written? No idea, but this is how dotnetzip allows this operation. – Jon May 11 '12 at 07:40
  • For bits i think `latest size - earlier size` will give you `added size` – Nikhil Agrawal May 11 '12 at 07:44
  • never mind, I was just curious about the possibility of inserting files into an archive, since my understanding of compressed files demands a compression of the whole file again to be effective. But wikipedia told me that there are some accepted redundancies for the sake of utility. – sschrass May 11 '12 at 07:46
  • 1
    To my mind AddFile just inculcate unzip, add and zip again. Some body who has this library please, check in reflector. – RredCat May 11 '12 at 07:46
0

Working with Zip Files in .NET

Tilak
  • 30,108
  • 19
  • 83
  • 131