title: How to detect CRC errs in an file when extracting from a zip
body: <p>I work for a company that has a cloud base application that allows users to transfer data files from the PC to their mobile device and back again allowing the users to update and edited their files out in the field. </p>
  
  <p>Randomly I will run across an issue where during the transfer of the file through the interwebs, the zip structure gets damaged and a random file in the zip will have crc errors. </p>
  
  <p>Typically it will be an image, but not always. This causes issues with our software and prevents the files from loading. I wrote a tool that scans the zip file and looks for and repairs errors in the xml files in the zip; however, the System.IO.Compression.ZipFile.ExtractToDirectory function is not throwing an errors, it extracts the file and writes it out. </p>
  
  <p>The System.IO.Compression.ZipFile.CreateFromDirectory call I make later then zips up this file, again without error resulting in a zip that still has a crc error and still will not open in our software.</p>
  
  <p>So here is my connumdry, How can I check the files I am extracting from the zip for crc errors and not place them in the new zip once i am done with my other work?</p>
  
  <p>I have tried a few things and short of loading every file type in to a reader of some sort (there are a dozen or so file types that may be in the zip) I got nothing.</p>
  
  <p>P.S. I am very new to C# and have no formal coding training so I may ask some dumb questions, sorry. :D</p>
  
  <p>An example can be found <a href="https://alamodeinc-my.sharepoint.com/:u:/g/personal/jason_k_alamode_com/EaR4SLO1f6BIjwC8rH3FuaIB-_q4jcehjAnmZTiVgYwSVw?e=g3oFtd" rel="nofollow noreferrer">here</a> </p>
  
  <p>OK I tried what was suggested and here is what I get</p>
  
  <p>Using the following code:</p>
  
  <pre><code>//https://johnlnelson.com/tag/zip-archive-c/ using ZipArchive
  string zipFile = FileName;
  string extractPath = @"C:\Temp\XAP\" + xapName;
  ZipArchive zipArchive = ZipFile.OpenRead(zipFile);
  
  if (zipArchive.Entries != null &amp;&amp; zipArchive.Entries.Count &gt; 0)
  {
  foreach (ZipArchiveEntry entry in zipArchive.Entries)
  {
  entry.ExtractToFile(System.IO.Path.Combine(extractPath, entry.FullName));
  }
  }
  </code></pre>
  
  <p>and it generates the following error </p>
  
  <pre><code>ERROR:
  System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Temp\XAP\CRC in bmp\assignmentmap.bmp'.
     at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
     at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
     at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
     at System.IO.Compression.ZipFile
  </code></pre>
  
  <p>The code fails to extract any of the files in the zip not just the corrupted one.</p>