locked
How to ZIp multiple folders along with subfolders in a folder into one zip RRS feed

  • Question

  • I have develope a one application in that application i had a requirement to zip a folder and its subfolder into one zip through programatically using c#.net but not to use the third party controls to zip a folder, using only c#.net to zip a folder along with its subfolder into one zip.Please help me out to solve this issue.

    please dont give any links,bcoz i searched and wasted lot of time to do this task, please give the code to zip a folder.

     

     

    Saturday, June 4, 2011 8:07 AM

Answers

  • There is no support for creating standard ZIP files in the .NET framework. You can use the System.Packaging.IO.ZipPackage to create a zip file, but this doesn't create standard ZIP archives, as these classes are meant to built the new Office 2007 file format packages.

    In the past you could also use the J# Java IO Zip classes, but J# is now deprecated, plus it isn't available on all systems by default.

    http://msdn.microsoft.com/en-us/magazine/cc164129.aspx

    There are 3rd party libraries that can do this, SharpZipLib is a free and open source solution that has been around a long time and works good:

    http://sharpdevelop.net/OpenSource/SharpZipLib/

    DotNetZip is also stable and open source:

    http://dotnetzip.codeplex.com/

    If your requirement not to use 3rd party controls comes from a business requirement, you could consider using the .NET Assembly Linker to merge the component into your own application. OR you could add all the source files to you project.

    As to your request for code, what is giving you problems? It is very easy to use Directory.EnumerateFiles and Directory.EnumerateDirectories to recursively do soemthing with all files in a directory and all directories below it. Or is your issue actually adding files to a ZIP archive.

    The part that actually builds the ZIP file depends on the solution you've chosen

    The the recursive directory enumeration looks like this:

                string basePath = @"e:\";

                Queue<string> subDirectories = new Queue<string>();
                subDirectories.Enqueue(basePath);

                string path = null;
                while (subDirectories.Count > 0)
                {
                    path = subDirectories.Dequeue();

                    foreach (var file in Directory.EnumerateFiles(basePath))
                    {
                        // Add file to Zip
                        // If you need the relative path to the file or directory, use 
                        // http://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path
                    }

                    foreach (var subDirectory in Directory.EnumerateDirectories(basePath))
                    {
                        subDirectories.Enqueue(subDirectory);
                    }
                }
            }

    Saturday, June 4, 2011 9:27 AM
  • Make an empty zip archive by writing PK56 + 18 zeros to a file.  Use shell32.CopyHere to compress files and folders into it.  Reference shell32 if you may.  If not, use late binding.
    Saturday, June 4, 2011 2:03 PM

All replies

  • There is no support for creating standard ZIP files in the .NET framework. You can use the System.Packaging.IO.ZipPackage to create a zip file, but this doesn't create standard ZIP archives, as these classes are meant to built the new Office 2007 file format packages.

    In the past you could also use the J# Java IO Zip classes, but J# is now deprecated, plus it isn't available on all systems by default.

    http://msdn.microsoft.com/en-us/magazine/cc164129.aspx

    There are 3rd party libraries that can do this, SharpZipLib is a free and open source solution that has been around a long time and works good:

    http://sharpdevelop.net/OpenSource/SharpZipLib/

    DotNetZip is also stable and open source:

    http://dotnetzip.codeplex.com/

    If your requirement not to use 3rd party controls comes from a business requirement, you could consider using the .NET Assembly Linker to merge the component into your own application. OR you could add all the source files to you project.

    As to your request for code, what is giving you problems? It is very easy to use Directory.EnumerateFiles and Directory.EnumerateDirectories to recursively do soemthing with all files in a directory and all directories below it. Or is your issue actually adding files to a ZIP archive.

    The part that actually builds the ZIP file depends on the solution you've chosen

    The the recursive directory enumeration looks like this:

                string basePath = @"e:\";

                Queue<string> subDirectories = new Queue<string>();
                subDirectories.Enqueue(basePath);

                string path = null;
                while (subDirectories.Count > 0)
                {
                    path = subDirectories.Dequeue();

                    foreach (var file in Directory.EnumerateFiles(basePath))
                    {
                        // Add file to Zip
                        // If you need the relative path to the file or directory, use 
                        // http://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path
                    }

                    foreach (var subDirectory in Directory.EnumerateDirectories(basePath))
                    {
                        subDirectories.Enqueue(subDirectory);
                    }
                }
            }

    Saturday, June 4, 2011 9:27 AM
  • Make an empty zip archive by writing PK56 + 18 zeros to a file.  Use shell32.CopyHere to compress files and folders into it.  Reference shell32 if you may.  If not, use late binding.
    Saturday, June 4, 2011 2:03 PM
  • Thank you for your reply, i will try with these links
    Monday, June 6, 2011 5:34 AM
  • Hi ferozv9,

    I temporary make Jesse’s reply and JohnWein’s reply as answer. You can unmark it if they provide no help

    Thank you for your understanding.

     

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 14, 2011 8:26 AM
    Moderator