如何使用DeflateStream类压缩和解压缩目录而不是文件目录。 [英] How to use the DeflateStream class to compress and decompress a directory not directory of files.

查看:85
本文介绍了如何使用DeflateStream类压缩和解压缩目录而不是文件目录。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用msdn代码逐个压缩目录中的文件但我需要将整个目录(压缩文件夹)压缩到一个cmp文件。

这是msdn代码:

I used msdn code that compress files inside a directory one by one but I need to compress the whole directory (compress a folder) to one cmp file.
this is the msdn code:

using System;
using System.IO;
using System.IO.Compression;

public class Program
{

    public static void Main()
    {
        // Path to directory of files to compress and decompress.
        string dirpath = @"c:\users\public\reports";

        DirectoryInfo di = new DirectoryInfo(dirpath);

        // Compress the directory's files.
        foreach (FileInfo fi in di.GetFiles())
        {
            Compress(fi);
    	}

        // Decompress all *.cmp files in the directory.
        foreach (FileInfo fi in di.GetFiles("*.cmp"))
        {
    		Decompress(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 != ".cmp")
            {
                // Create the compressed file.
                using (FileStream outFile = 
                        File.Create(fi.FullName + ".cmp"))
                {
                    using (DeflateStream Compress = 
                        new DeflateStream(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());
                    }
                }
            }
        }
    }

    public static void Decompress(FileInfo fi)
    {
        // Get the stream of the source file.
        using (FileStream inFile = fi.OpenRead())
        {
        	// Get original file extension, 
            // for example "doc" from report.doc.cmp.
        	string curFile = fi.FullName;
        	string origName = curFile.Remove(curFile.Length 
                    - fi.Extension.Length);

        	//Create the decompressed file.
        	using (FileStream outFile = File.Create(origName))
        	{
        	    using (DeflateStream Decompress = new DeflateStream(inFile,
        		    CompressionMode.Decompress))
        	    {
                        // Copy the decompression stream 
                        // into the output file.
            		    Decompress.CopyTo(outFile);
            		
            		Console.WriteLine("Decompressed: {0}", fi.Name);
        	    }
        	}
        }
    }
}

推荐答案

你别。文件夹没有任何内容,因此无需压缩。文件有内容。



你逐个添加每个文件,而不是文件夹。



也许您是否正在寻找将整个目录结构添加到.ZIP?
You don't. Folders don't have any content so there's nothing to compress. Files have content.

You add each file, one by one, not a folder.

Perhaps you're looking for adding an entire directory structure to the .ZIP?


这篇关于如何使用DeflateStream类压缩和解压缩目录而不是文件目录。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆