Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My problem is: how can I decompress a file after searching in a specific directory and sub folders for files with a specific extension.
please help

Line 146 --> error value cannot be null...

What I have tried:

C#
  1  <pre>using Microsoft.VisualBasic;
  2  using Microsoft.VisualBasic.CompilerServices;
  3  using System;
  4  using System.Collections;
  5  using System.Collections.Generic;
  6  using System.ComponentModel;
  7  using System.Data;
  8  using System.Drawing;
  9  using System.IO;
 10  using System.IO.Compression;
 11  using System.Linq;
 12  using System.Runtime.CompilerServices;
 13  using System.Text;
 14  using System.Threading.Tasks;
 15  using System.Windows.Forms;
 16  using System.Xml;
 17  
 18  namespace SD
 19  {
 20      public partial class Form1 : Form
 21      {
 22          public Form1()
 23          {
 24              InitializeComponent();
 25          }
 26          private string fileName;
 27  
 28          private void Form1_Load(object sender, EventArgs e)
 29          {
 30             
 31          }
 32  
 33          private void button1_Click(object sender, EventArgs e)
 34          {
 35              List<string> filenames = FindFiles(path.Text, exten.Text,check.Checked);
 36              lstFiles.Items.Clear();
 37              foreach (string filename in filenames)
 38              {
 39                  lstFiles.Items.Add(filename);
 40              }
 41          }
 42          private List<string> FindFiles(string dir_name, string patterns, bool search_subdirectories)
 43          {
 44              List<string> files = new List<string>();
 45              string[] pattern_array = patterns.Split(';');
 46              SearchOption search_option = SearchOption.AllDirectories;
 47              foreach (string pattern in pattern_array)
 48              {
 49                  foreach (string filename in Directory.GetFiles(dir_name, pattern, search_option))
 50                  {
 51                      if (!files.Contains(filename)) files.Add(filename);
 52                  }
 53              }
 54              label1.Text = files.Count.ToString();
 55              files.Sort();
 56              return files;
 57          }
 58  
 59          private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
 60          {
 61  
 62              
 63          }
 64          private byte[] DecompressArray(byte[] content)
 65          {
 66              byte[] array;
 67              int num;
 68              using (MemoryStream memoryStream = new MemoryStream())
 69              {
 70                  using (MemoryStream memoryStream1 = new MemoryStream(content))
 71                  {
 72                      using (GZipStream gZipStream = new GZipStream(memoryStream1, CompressionMode.Decompress))
 73                      {
 74                          byte[] numArray = new byte[1024];
 75                          do
 76                          {
 77                              num = gZipStream.Read(numArray, 0, (int)numArray.Length);
 78                              memoryStream.Write(numArray, 0, num);
 79                          }
 80                          while (num > 0);
 81                      }
 82                  }
 83                  array = memoryStream.ToArray();
 84              }
 85              return array;
 86          }
 87          private void UnzipDirectory(byte[] compressed, string outputPath)
 88          {
 89              IEnumerator enumerator = null;
 90              IEnumerator enumerator1 = null;
 91              if (!Directory.Exists(outputPath))
 92              {
 93                  Directory.CreateDirectory(outputPath);
 94              }
 95              byte[] numArray = this.DecompressArray(compressed);
 96              XmlDocument xmlDocument = new XmlDocument();
 97              using (MemoryStream memoryStream = new MemoryStream(numArray))
 98              {
 99                  xmlDocument.Load(memoryStream);
100                  XmlNode firstChild = xmlDocument.FirstChild;
101                  XmlNode xmlNodes = firstChild.FirstChild;
102                  XmlNode nextSibling = firstChild.FirstChild.NextSibling;
103                  try
104                  {
105                      enumerator = xmlNodes.ChildNodes.GetEnumerator();
106                      while (enumerator.MoveNext())
107                      {
108                          object objectValue = RuntimeHelpers.GetObjectValue(enumerator.Current);
109                          Directory.CreateDirectory(Path.Combine(outputPath, ((XmlNode)objectValue).Attributes.Item(0).Value));
110                      }
111                  }
112                  finally
113                  {
114                      if (enumerator is IDisposable)
115                      {
116                          (enumerator as IDisposable).Dispose();
117                      }
118                  }
119                  try
120                  {
121                      enumerator1 = nextSibling.ChildNodes.GetEnumerator();
122                      while (enumerator1.MoveNext())
123                      {
124                          object obj = RuntimeHelpers.GetObjectValue(enumerator1.Current);
125                          string str = Path.Combine(outputPath, ((XmlNode)obj).Attributes.Item(0).Value);
126                          byte[] numArray1 = Convert.FromBase64String(((XmlNode)obj).InnerText);
127                          File.WriteAllBytes(str, numArray1);
128                      }
129                  }
130                  finally
131                  {
132                      if (enumerator1 is IDisposable)
133                      {
134                          (enumerator1 as IDisposable).Dispose();
135                      }
136                  }
137              }
138          }
139          private void lstFiles_DoubleClick(object sender, EventArgs e)
140          {
141  
142              if (lstFiles.SelectedItem != null)
143              {
144  
145                  lstFiles.SelectedItem.ToString();
146                  FileInfo fileInfo = new FileInfo(fileName);
147                  try
148                  {
149                      if (File.Exists(fileName))
150                      {
151                          UnzipDirectory(File.ReadAllBytes(fileName), fileInfo.DirectoryName);
152                      }
153                  }
154                  catch (Exception exception)
155                  {
156                      ProjectData.SetProjectError(exception);
157                      Interaction.MsgBox("error", MsgBoxStyle.OkOnly, null);
158                      ProjectData.ClearProjectError();
159                      return;
160                  }
161                  Interaction.MsgBox("Complete", MsgBoxStyle.OkOnly, null);
162              }
163          }
164      }
165  }
Posted
Updated 14-Apr-20 3:11am
v5
Comments
Patrice T 13-Apr-20 6:32am    
What is the problem with your code ?
Stm21 13-Apr-20 6:36am    
the problem is i don't know how can i start the decompression process for all files
Patrice T 13-Apr-20 6:44am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Richard MacCutchan 13-Apr-20 6:32am    
What is the problem?

1 solution

You can use Directory.GetFiles() as in this example: https://www.dotnetperls.com/recursive-file-list[^]

To unzip a .zip file, see example here: ZipFile.ExtractToDirectory Method (System.IO.Compression) | Microsoft Docs[^]

If you have other formats than .zip your can call a tool like 7Zip from your application:
command-line-archive-tool-wrappers-tar-zip-rar-arj-7z-etc~7-zip[^]
 
Share this answer
 
v3
Comments
Maciej Los 13-Apr-20 11:07am    
5ed!
RickZeeland 13-Apr-20 12:12pm    
Thanks, and beware of the Easter eggs :)
Maciej Los 13-Apr-20 12:21pm    
Thanks ;)
Happy Easter!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900