Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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);
        	    }
        	}
        }
    }
}
Posted

1 solution

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?
 
Share this answer
 
Comments
MohammadHG 2-Sep-14 10:30am    
Is there any way else to compress all files togather in one cmp file?
Dave Kreskowiak 2-Sep-14 12:14pm    
Sorry, I misunderstood what you were talking about.

Defalte kind of sucks as it only does one stream at a time. Read http://www.codeproject.com/Articles/313790/NET-Native-Multiple-File-Archive-Library for more information on how to use it to do multiple files.

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