How to compress files

前端 未结 10 927
渐次进展
渐次进展 2021-02-01 06:58

I want to compress a file and a directory in C#. I found some solution in Internet but they are so complex and I couldn\'t run them in my project. Can anybody suggest me a clear

相关标签:
10条回答
  • 2021-02-01 07:37

    Using DotNetZip http://dotnetzip.codeplex.com/, there's an AddDirectory() method on the ZipFile class that does what you want:

    using (var zip = new Ionic.Zip.ZipFile())
    {
        zip.AddDirectory("DirectoryOnDisk", "rootInZipFile");
        zip.Save("MyFile.zip");
    }
    

    Bonne continuation...

    0 讨论(0)
  • 2021-02-01 07:40

    For .Net Framework 4.5 this is the most clear example I found:

    using System;
    using System.IO;
    using System.IO.Compression;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                string startPath = @"c:\example\start";
                string zipPath = @"c:\example\result.zip";
                string extractPath = @"c:\example\extract";
    
                ZipFile.CreateFromDirectory(startPath, zipPath);
    
                ZipFile.ExtractToDirectory(zipPath, extractPath);
            }
        }
    }
    

    You'll need to add a reference to System.IO.Compression.FileSystem

    From: https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-compress-and-extract-files

    0 讨论(0)
  • 2021-02-01 07:47

    just use following code for compressing a file.

           public void Compressfile()
            {
                 string fileName = "Text.txt";
                 string sourcePath = @"C:\SMSDBBACKUP";
                 DirectoryInfo di = new DirectoryInfo(sourcePath);
                 foreach (FileInfo fi in di.GetFiles())
                 {
                     //for specific file 
                     if (fi.ToString() == fileName)
                     {
                         Compress(fi);
                     }
                 } 
            }
    
    public static void Compress(FileInfo fi)
            {
                // Get the stream of the source file.
                using (FileStream inFile = fi.OpenRead())
                {
                    // Prevent compressing hidden and 
                    // already compressed files.
                    if ((File.GetAttributes(fi.FullName)
                        & FileAttributes.Hidden)
                        != FileAttributes.Hidden & fi.Extension != ".gz")
                    {
                        // Create the compressed file.
                        using (FileStream outFile =
                                    File.Create(fi.FullName + ".gz"))
                        {
                            using (GZipStream Compress =
                                new GZipStream(outFile,
                                CompressionMode.Compress))
                            {
                                // Copy the source file into 
                                // the compression stream.
                                inFile.CopyTo(Compress);
    
                                Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                                    fi.Name, fi.Length.ToString(), outFile.Length.ToString());
                            }
                        }
                    }
                }
            }
    
        }
    
    0 讨论(0)
  • 2021-02-01 07:48

    You could use GZipStream in the System.IO.Compression namespace

    .NET 2.0.

    public static void CompressFile(string path)
    {
        FileStream sourceFile = File.OpenRead(path);
        FileStream destinationFile = File.Create(path + ".gz");
    
        byte[] buffer = new byte[sourceFile.Length];
        sourceFile.Read(buffer, 0, buffer.Length);
    
        using (GZipStream output = new GZipStream(destinationFile,
            CompressionMode.Compress))
        {
            Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
                destinationFile.Name, false);
    
            output.Write(buffer, 0, buffer.Length);
        }
    
        // Close the files.
        sourceFile.Close();
        destinationFile.Close();
    }
    

    .NET 4

    public static void Compress(FileInfo fi)
        {
            // Get the stream of the source file.
            using (FileStream inFile = fi.OpenRead())
            {
                // Prevent compressing hidden and 
                // already compressed files.
                if ((File.GetAttributes(fi.FullName) 
                    & FileAttributes.Hidden)
                    != FileAttributes.Hidden & fi.Extension != ".gz")
                {
                    // Create the compressed file.
                    using (FileStream outFile = 
                                File.Create(fi.FullName + ".gz"))
                    {
                        using (GZipStream Compress = 
                            new GZipStream(outFile, 
                            CompressionMode.Compress))
                        {
                            // Copy the source file into 
                            // the compression stream.
                        inFile.CopyTo(Compress);
    
                            Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                                fi.Name, fi.Length.ToString(), outFile.Length.ToString());
                        }
                    }
                }
            }
        }
    

    http://msdn.microsoft.com/en-us/library/ms404280.aspx

    0 讨论(0)
提交回复
热议问题