none
Creating zip files RRS feed

  • Question

  • Hi,

    I have some folders under D:\Out_Conv. I wanted zip all the folders under D:\Out_Conv and put the zipped folders under different path. Can anyone please guide me how can I achieve this pragmatically. Also, it would be great if anyone how can I achieve this by running in command prompt.

    Kindly waiting for your response.

    Thanks,

    Santosh

    Thursday, November 14, 2019 7:05 AM

All replies

  • This is quite easy when using System.IO.Compression:

    using System.IO.Compression;
    
    // ...
    
    var myDir = @"D:\Out_Conv\";
    var myZip = @"D:\zipped.zip";
    
    ZipFile.CreateFromDirectory(myDir, myZip);
    

    Thursday, November 14, 2019 7:52 AM
  • Hi Santosh Umarani, 

    Thank you for posting here.

    For your question, you want to compress file using C#.

    Here’s the code of my test, and you can refer to it.

                string startPath = @"D:\TestFile";
                string zipPath = @"D:\TestFile2\";
                string savePath;
                DirectoryInfo directoryInfo = new DirectoryInfo(startPath);
                DirectoryInfo[] paths = directoryInfo.GetDirectories();
                for (int i=0;i< paths.Length;i++)
                {
                    savePath = zipPath + paths[i].Name + ".zip";
                    ZipFile.CreateFromDirectory(paths[i].FullName, savePath);
                }

    Folders in Test File:

    Result:

    If you want to save in different files, you need to specify ‘savePath’ for each folder.

    You can also refer to the following reference.

    How to: Compress and extract files

    Besides, here’s a reference about use command line to compress file.

    How to zip a file using cmd line?

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Thursday, November 14, 2019 8:08 AM
    Moderator
  • Thank you very much for the response.

    I've 7zip installed in my machine. Hence, 'ZipFile' is not getting recognized even after using System.IO.Compression namespace. Can you please let me know is there any different namespace and property for 7zip?

    Kindly waiting for your response.

    Thursday, November 14, 2019 8:29 AM
  • 7zip ist 'just' an application on your PC (I suppose) and no API to use withing C#.

    According to MS Docs you have to add references to System.IO.Compression and System.IO.Compression.FileSystem to your Project to use ZipFile

    Thursday, November 14, 2019 10:21 AM

  • I've 7zip installed in my machine. Hence, 'ZipFile' is not getting recognized even after using System.IO.Compression namespace. Can you please let me know is there any different namespace and property for 7zip?


    Using the suggested ZipFile method will likely be the most portable to other
    computers, and easiest to code. However if you *really* want to use 7-Zip for 
    the task, see this thread:

    How do I create 7-Zip archives with .NET?
    https://stackoverflow.com/questions/222030/how-do-i-create-7-zip-archives-with-net

    Also see:

    LZMA SDK (Software Development Kit)
    https://www.7-zip.org/sdk.html

    - Wayne

    Thursday, November 14, 2019 1:35 PM