I am currently working with SharpZipLib under .NET 2.0, and through this I need to compress one file into one compressed archive. For this, I am currently using the following:
string tempFilePath = @"C:\Users\Username\AppData\Local\Temp\tmp9AE0.tmp.xml"; string archiveFilePath = @"C:\Archive\Archive_[UTC TIMESTAMP].zip"; FileInfo inFileInfo = new FileInfo(tempFilePath); ICSharpCode.SharpZipLib.Zip.FastZip fZip = new ICSharpCode.SharpZipLib.Zip.FastZip(); fZip.CreateZip(archiveFilePath, inFileInfo.Directory.FullName, false, inFileInfo.Name);
This works exactly (ish), as it should be, however, during testing, I came across a minor error. Suppose my temp directory (i.e. the directory containing the uncompressed input file) contains the following files:
tmp9AE0.tmp.xml //The input file I want to compress xxx_tmp9AE0.tmp.xml // Some other file yyy_tmp9AE0.tmp.xml // Some other file wibble.dat // Some other file
When I start compression, all .xml included in the compressed archive. The reason for this is because the last fileFilter parameter was fileFilter to the CreateZip method. Under the hood, SharpZipLib performs pattern matching, and it also captures files with the xxx_ and yyy_ . I guess he will also pick up something postfix.
So the question is how to compress a single file using SharpZipLib? And again, maybe the question is how can I format this fileFilter so that a match can only ever pick up the file that I want to compress, and nothing more.
Aside, is there a reason why System.IO.Compression does not include the ZipStream class? (It only supports GZipStream)
EDIT: Solution (derived from an accepted answer from Hans Passant)
This is the compression method I implemented:
private static void CompressFile(string inputPath, string outputPath) { FileInfo outFileInfo = new FileInfo(outputPath); FileInfo inFileInfo = new FileInfo(inputPath);
c # compression sharpziplib
MrEyes
source share