66923

C# SharpZipLib strips irrelevant directory names

Question:

I am using <a href="http://www.icsharpcode.net/OpenSource/SharpZipLib/" rel="nofollow">SharpZipLib</a> to zip up a folder with subdirectories and this is working fine. What I would like to do is strip off the parents directories of the first child file so the whole structure that is irrelevant isn't carried forth...

Example:

c:\a\b\c\d\e\f\g\h\file1.txt

c:\a\b\c\d\e\f\g\h\file2.txt

c:\a\b\c\d\e\f\g\h\i\file1.txt

c:\a\b\c\d\e\f\g\h\i\file2.txt

It should end up like this:

<blockquote>

file1.txt

file2.txt

i\file1.txt

i\file2.txt

</blockquote>

How can I do this?

Here is the code I have so far:

ZipFile zipFile = new ZipFile(destinationArchive); zipFile.BeginUpdate(); foreach (FileInfo file in sourceFiles) { zipFile.Add(file.FullName); } zipFile.CommitUpdate(); zipFile.Close();

Answer1:

Use ZipOutputStream instead:

string[] sourceFiles = new [] { @"c:\a\b\c\d\e\f\g\h\file1.txt", @"c:\a\b\c\d\e\f\g\h\i\file1.txt" }; FileStream fileStream = File.Create(@"c:\temp\test.zip"); ZipOutputStream zipOut = new ZipOutputStream(fileStream); string baseDir = @"c:\a\b\c\d\e\f\g\h\"; foreach (var sourceFile in sourceFiles) { ZipEntry entry = new ZipEntry(sourceFile.Replace(baseDir,"")); zipOut.PutNextEntry(entry); FileStream inFile = File.OpenRead(sourceFile); byte[] buffer = new byte[8192]; int bytesRead = 0; while ((bytesRead = inFile.Read(buffer, 0, buffer.Length)) > 0) { zipOut.Write(buffer,0,bytesRead); } zipOut.CloseEntry(); } zipOut.Close();

Answer2:

Or look on CodePlex for DotNetZip.

Recommend

  • Script Prompt Windows - List Files
  • Extract found line + subsequent line from a text file
  • Without editing the csproj file, get MSBuild to copy all files marked as Content to a folder, preser
  • Loop a line command in bash
  • How do I add the src directory instead of the individual files?
  • Calculating average without considering missing values in shell script?
  • How to check if arguments are legit?
  • Find for a class file in Linux that is present in JAR
  • Insert Date to MySql using c#
  • Unable to use System.IO.Compression.FileSystem.dll
  • Error when I try to generate a libgdx projet
  • archiving symlinks with python zipfile
  • Archiving a zip file and save it to a location of choice, with no extra file paths?
  • MSBuild: OutputPath directory is empty
  • Obtaining the “default” mockup TinyMCE configuration on Plone 5
  • How to save dynamically created textboxes and their values
  • Unzip archive in .Net CORE 1.0
  • Setting the run time properties on SpringApplicationBuilder()
  • pip in virtualenv gets ConnectTimeoutError
  • how to set variables in a php include file?
  • How to print columns containing value
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Installed module is empty
  • Deleting and Updating values from a cusrsor adapter
  • Is possible to count alias result on mysql
  • Importing jscolor library in angular 2
  • To display the title for the current loaction in map in iphone
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • php design question - will a Helper help here?
  • KeystoneJS: Relationships in Admin UI not updating
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Acquiring multiple attributes from .xml file in c#
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal