We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

rengber on 08/07/07


Tagged

archive zip hashTable SharpZipLib


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

ad5qa


Building a Hashtable of File Contents from a Zip File Containing Multiple Files and Folders


Published in: C# 


  1. public static Hashtable BuildFileTable(Stream s)
  2. {
  3. Hashtable retVal = new Hashtable();
  4. ZipInputStream zips = new ZipInputStream(s);
  5. ZipEntry ze = zips.GetNextEntry();
  6. while (ze != null)
  7. {
  8. if (ze.IsFile)
  9. {
  10. retVal.Add(ze.Name, GetContentFromZipEntry(ze, zips));
  11. }
  12. ze = zips.GetNextEntry();
  13. }
  14. return retVal;
  15. }
  16.  
  17. public static string GetContentFromZipEntry(ZipEntry ze, ZipInputStream zips)
  18. {
  19. string retVal = string.Empty;
  20. if (ze.Offset > int.MaxValue || ze.Size > int.MaxValue)
  21. {
  22. throw new ApplicationException("Files larger than 4gb not supported.");
  23. }
  24. Byte[] buffer = new byte[ze.Size];
  25. int numRead = zips.Read(buffer, Convert.ToInt32(ze.Offset), buffer.Length);
  26. MemoryStream ms = new MemoryStream(buffer);
  27. StreamReader sr = new StreamReader(ms);
  28. retVal = sr.ReadToEnd().Trim();
  29.  
  30. return retVal;
  31. }

Report this snippet 

You need to login to post a comment.