Home > OS >  How to way extract format .gz/.zip/.rar/.tar.gz in .net core mvc c#
How to way extract format .gz/.zip/.rar/.tar.gz in .net core mvc c#

Time:12-15

I have issue when action open file follow format .gz/.tar.gz This is image errorenter image description here

This is code:

 public void Check(string path)
    {
        foreach (var fileName in Directory.GetFiles(path))
        {
            if (Path.GetExtension(fileName) == ".gz" && Path.GetFileNameWithoutExtension(fileName).Contains("tar"))
            {
                Console.WriteLine(fileName);

                ExtractTGZ(fileName, Path.GetDirectoryName(fileName)   @"\"   Path.GetFileNameWithoutExtension(fileName));
                System.IO.File.Delete(fileName);

            }
        }
        
    }

CodePudding user response:

The System.IO.Compression namespace contains the following types for compressing and decompressing files and streams. You can also use these types to read and modify the contents of a compressed file.

string fileName = Guid.NewGuid()   ifile.FileName;
string path = Path.Combine(FilesTempPath, fileName);
using (var fileStream = new FileStream(path, FileMode.Create))
{
    ifile.CopyTo(fileStream);
}

var FileZipPath = FilesTempPath   "\\"   fileName;
ZipFile.ExtractToDirectory(FileZipPath, FilesTempPath);

CodePudding user response:

The following example from MSDN shows how to use the GZipStream class to compress and decompress a directory of files.

  • Related