Archive for the ‘C#’ Category

my first c# test, extracting and renaming first entrty in zip files

Tuesday, December 19th, 2006

I’ve never done any c# coding before, but honest to say, I do have been a (tiny) bit curious on it. It looks a lot like my old favourite java, and it almost smells like java.

My case now was that I needed to extract the first instance in ~1500 zip files from a folder in windows and rename each extracted file to the same name as the zip file (not the extension name of cource). Furthermore the zipfiles where named with “two file extensions” meaning they could be called blabla.eps.zip (or even worse – blabla.jpg.zip which is really odd).

Anyway. As a none Microsoftish or .netish guy in most aspects I found a great IDE/RAD for c# that is GPL:d – SharpDevelop. The install file is around 4.5mb (compared to MS Visual studio for C# cd image beign ~440mb). They also ship a native GPL:d zip library (http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx) for C# that’s really easy to use.

Screen capture of Sharpdevelop

My small but efficient result examplifies how to read a directory, file info, zip contents and how to extract zipfiles:

  1.  
  2.       textBoxResults.Text = "Starting to process zip files in dir " + textBoxDir.Text + Environment.NewLine;
  3.       DirectoryInfo ourDir = new DirectoryInfo(textBoxDir.Text);
  4.       FileInfo[] ourZipFiles = ourDir.GetFiles("*.zip");
  5.       int noFiles = ourZipFiles.Length;
  6.       textBoxResults.Text += "Found " + noFiles + " zip files to extract:" + Environment.NewLine;
  7.       for (int y=0;y<ourZipFiles.Length;y++) {
  8.         textBoxResults.Text += "Extracting " + ourZipFiles[y].FullName + ".. ";
  9.         //extract contents
  10.         //give us a zip rep
  11.         try {
  12.           ZipFile zf = new ZipFile(ourZipFiles[y].FullName);
  13.           //get first entry and extract it..
  14.           foreach (ZipEntry ze in zf) {
  15.             if (ze.CanDecompress && ze.IsFile) {
  16.               //ok. we can extract something. lets do it then. set up fastzip to do it..
  17.               FastZip fz = new FastZip();
  18.               fz.ExtractZip(zf.Name, textBoxOutDir.Text,ze.Name);
  19.               FileInfo tmpfi = new FileInfo(textBoxOutDir.Text + ze.Name);
  20.               //change file extension
  21.               string full = tmpfi.FullName;
  22.               //textBoxResults.Text += "FULL: " + full + Environment.NewLine + " short: " +  + Environment.NewLine;
  23.               string newFileName = tmpfi.Directory.FullName + "" + ourZipFiles[y].Name.Substring(0,ourZipFiles[y].Name.Length-7) + tmpfi.Name.Substring(tmpfi.Name.Length-3);
  24.               //rename
  25.               try {
  26.               //  textBoxResults.Text += "from filename " + tmpfi.FullName + " to " +newFileName;
  27.                 tmpfi.MoveTo(newFileName);
  28.               } catch (IOException ioex) {
  29.                 throw new ZipException("can’t move file; extracted file not found " + ioex.Message);
  30.               }
  31.               textBoxResults.Text +=  ze.Name + " (renamed to " + newFileName + ") ";
  32.               break; //break iteration after first valid zip entry. for now we only want the first one..
  33.             }
  34.           }
  35.           textBoxResults.Text += "..done :) "+ Environment.NewLine;
  36.         } catch(ZipException zex) {
  37.           textBoxResults.Text += "..failed :( ERROR CAUSE: " + zex.Message + Environment.NewLine;
  38.         }
  39.        
  40.       }
  41.  

To summarize I use the following components (not noting gui stuff):
* DirectoryInfo – read/modify directory data
* FileInfo – read/modify file data (not content)
* ZipFile – a zip file representation and functions.
* ZipEntry – a file or folder in a ZipFile.
* FastZip – easy to use utility to ie extract files from zip.

Also note the use of Environment.NewLine to create correct newline/carriage return (std \n\r) in a string. … I tried to use escape style but that seems not to be valid..

Go ahead and download full source code including my sharpdevelop project file attached to this post as zip archive. MultiZipExtractor source codes

Note that to use the ziplibrary you need to add the compiled version as a reference. In sharpdevelop that’s done by selecting the project tab to the left, and right click on references in your project. Browse the dll file and add it. Then just add an extra use line in you code.