0

What I want to do is create a zip folder by right-clicking in windows explorer, and new -> Compressed (zip) folder.

Then I want to use something like My.Computer.Filesystem.MoveFile("C:\From.jpg", "C:\ZipFolder.zip\To.jpg", true)

But I get an error "file or directory with the same name already exists".

Is there an easy way to do this? I thought windows supported zip folders but it seems to have no idea that I'm trying to move the file to a zip folder, rather, it is seeing the zip as a file and thinks I'm overwriting it or something.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Aaron Brown
  • 281
  • 1
  • 5
  • 13
  • Side note: Windows never supported "zip folders" - there is file system level compression and creation of Zip files/opening of ZIP files in explorer to see content... – Alexei Levenkov Jun 24 '14 at 16:35
  • If this question is a duplicate, at least provide the link to where it was answered before. – Aaron Brown Jun 24 '14 at 16:39
  • If you don't see automatic "This question already has an answer here:" comment in your post please ask question on http://meta.stackoverflow.com why it did not show up for you. Here is the link just in case http://stackoverflow.com/questions/905654/zip-folder-in-c-sharp. – Alexei Levenkov Jun 24 '14 at 16:49

1 Answers1

1

In DotNetZip, adding files to an existing zip is really simple and reliable.

DotNetZip is a FAST, FREE class library and toolset for manipulating zip files. Use VB, C# or any .NET language to easily create, extract, or update zip files.

DotNetZip is the best open-source ZIP library for .NET.

using (var zip = ZipFile.Read(nameOfExistingZip))
{
    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
    zip.AddFile(additionalFileToAdd);
    zip.Save();
}

If you want to specify a directory path for that new file, then use a different overload for AddFile().

using (var zip = ZipFile.Read(nameOfExistingZip))
{
    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
    zip.AddFile(additionalFileToAdd, "directory\\For\\The\\Added\\File");
    zip.Save();
}

If you want to add a set of files, use AddFiles().

using (var zip = ZipFile.Read(nameOfExistingZip))
{
    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
    zip.AddFiles(listOfFilesToAdd, "directory\\For\\The\\Added\\Files");
    zip.Save();
}
MRebai
  • 5,344
  • 3
  • 33
  • 52