title: SharpZipLib : Compressing a single file to a single compressed file
body: <p>I am currently working with SharpZipLib under .NET 2.0 and via this I need to compress a single file to a single compressed archive.  In order to do this I am currently using the following:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>This works exactly (ish) as it should, however while testing I have encountered a minor gotcha.  Lets say that my temp directory (i.e. the directory that contains the uncompressed input file) contains the following files:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>When I run the compression all the <code>.xml</code> files are included in the compressed archive.  The reason for this is because of the final <code>fileFilter</code> parameter passed to the <code>CreateZip</code> method.  Under the hood SharpZipLib is performing a pattern match and this also picks up the files prefixed with <code>xxx_</code> and <code>yyy_</code>.  I assume it would also pick up anything postfixed as well.</p>
  
  <p>So the question is, how can I compress a single file with SharpZipLib?  Then again maybe the question is how can I format that <code>fileFilter</code> so that the match can only ever pick up the file I want to compress and nothing else.</p>
  
  <p>As an aside, is there any reason as to why <code>System.IO.Compression</code> not include a <code>ZipStream</code> class? (It only supports GZipStream)</p>
  
  <p>EDIT : Solution (Derived from accepted answer from Hans Passant)</p>
  
  <p>This is the compression method I implemented:</p>
  
  <pre><code>private static void CompressFile(string inputPath, string outputPath)
  {
      FileInfo outFileInfo = new FileInfo(outputPath);
      FileInfo inFileInfo = new FileInfo(inputPath);
  
      // Create the output directory if it does not exist
      if (!Directory.Exists(outFileInfo.Directory.FullName))
      {
          Directory.CreateDirectory(outFileInfo.Directory.FullName);
      }
  
      // Compress
      using (FileStream fsOut = File.Create(outputPath))
      {
          using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(fsOut))
          {
              zipStream.SetLevel(3);
  
              ICSharpCode.SharpZipLib.Zip.ZipEntry newEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(inFileInfo.Name);
              newEntry.DateTime = DateTime.UtcNow;
              zipStream.PutNextEntry(newEntry);
  
              byte[] buffer = new byte[4096];
              using (FileStream streamReader = File.OpenRead(inputPath))
              {
                  ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(streamReader, zipStream, buffer);
              }
  
              zipStream.CloseEntry();
              zipStream.IsStreamOwner = true;
              zipStream.Close();
          }
      }
  }
  </code></pre>