Create normal zip file programmatically

后端 未结 11 890
野的像风
野的像风 2020-12-05 00:06

I have seen many tutorials on how to compress a single file in c#. But I need to be able to create a normal *.zip file out of more than just one file. Is there anything in .

11条回答
  •  甜味超标
    2020-12-05 00:21

    This can be done by adding a reference to System.IO.Compression and System.IO.Compression.Filesystem.

    A sample createZipFile() method may look as following:

    public static void createZipFile(string inputfile, string outputfile, CompressionLevel compressionlevel)
            {
                try
                {
                    using (ZipArchive za = ZipFile.Open(outputfile, ZipArchiveMode.Update))
                    {
                        //using the same file name as entry name
                        za.CreateEntryFromFile(inputfile, inputfile);
                    }
                }
                catch (ArgumentException)
                {
                    Console.WriteLine("Invalid input/output file.");
                    Environment.Exit(-1);
                }
    }
    

    where

    • inputfile= string with the file name to be compressed (for this example, you have to add the extension)
    • outputfile= string with the destination zip file name

    More details about ZipFile Class in MSDN

提交回复
热议问题