How can I unzip a file to a .NET memory stream?

前端 未结 6 595
鱼传尺愫
鱼传尺愫 2020-12-14 00:16

I have files (from 3rd parties) that are being FTP\'d to a directory on our server. I download them and process them even \'x\' minutes. Works great.

Now, some of th

6条回答
  •  离开以前
    2020-12-14 00:42

    Ok so combining all of the above, suppose you want to in a very simple way take a zip file called "file.zip" and extract it to "C:\temp" folder. (Note: This example was only tested for compress text files) You may need to do some modifications for binary files.

            using System.IO;
            using System.IO.Compression;
    
            static void Main(string[] args)
            {
                //Call it like this:
                Unzip("file.zip",@"C:\temp");
            }
    
            static void Unzip(string sourceZip, string targetPath)
            {
                using (var z = ZipFile.OpenRead(sourceZip))
                {
                    foreach (var entry in z.Entries)
                    {                    
                        using (var r = new StreamReader(entry.Open()))
                        {
                            string uncompressedFile = Path.Combine(targetPath, entry.Name);
                            File.WriteAllText(uncompressedFile,r.ReadToEnd());
                        }
                    }
                }
    
            }
    

提交回复
热议问题