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


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.