简体   繁体   中英

Zip file using C#

I want to zip one "CSV" file in to Zip file using C#.Net. Below i have written some code for create Zip file , using this code i am able to create zip file but after creating "Data1.zip" file extract manually means extracted file extension should be ".csv" but it is not coming.

        FileStream sourceFile = File.OpenRead(@"C:\Users\Rav\Desktop\rData1.csv");
        FileStream destFile = File.Create(@"C:\Users\Rav\Desktop\Data1.zip");

        GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress,false);

        try
        {
            int theByte = sourceFile.ReadByte();
            while (theByte != -1)
            {
                compStream.WriteByte((byte)theByte);
                theByte = sourceFile.ReadByte();
            }
        }
        finally
        {
            compStream.Dispose();
        }

http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx

This is gzip compression, and apparently it only compresses a stream, which when decompressed takes the name of the archive without the .gz extension. I don't know if I'm right here though. You might as well experiment with the code from MSDN, see if it works.

I used ZipLib for zip compression. It also supports Bz2, which is a good compression algorithm.

Use one of these libraries: http://www.icsharpcode.net/opensource/sharpziplib/ http://dotnetzip.codeplex.com/

I prefer #ziplib, but both are well documented and widely spread.

Use ICSharpCode.SharpZipLib(you can download it) and do the following

       private void CreateZipFile(string l_sFolderToZip)
       {
            FastZip z = new FastZip();
            z.CreateEmptyDirectories = true;
            z.CreateZip(l_sFolderToZip + ".zip", l_sFolderToZip, true, "");

            if (Directory.Exists(l_sFolderToZip))
                Directory.Delete(l_sFolderToZip, true);   

      }



        private void ExtractFromZip(string l_sFolderToExtract)
        {
            string l_sZipPath ="ur folder path" + ".zip";
            string l_sDestPath = "ur location" + l_sFolderToExtract;

            FastZip z = new FastZip();
            z.CreateEmptyDirectories = true;
            z.ExtractZip(l_sZipPath, l_sDestPath, "");

            if (File.Exists(l_sZipPath))
                File.Delete(l_sZipPath);
        }

Hope it helps...

Since .NET Framework 4.5, you can use the built-in ZipFile class (In the System.IO.Compression namespace).

public void ZipFiles(string[] filePaths, string zipFilePath)
{

    ZipArchive zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create);
    foreach (string file in filePaths)
    {
        zipArchive.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
    }
    zipArchive.Dispose();

}

Take a look at the FileSelectionManager library here: www.fileselectionmanager.com

First you have to add File Selection Manager DLL to your project

Here is an example for zipping:

class Program
{
    static void Main(string[] args)
    {
        String directory =  @"C:\images";
        String destinationDiretory =  @"c:\zip_files";
        String zipFileName =  "container.zip";        
        Boolean recursive =  true;
        Boolean overWrite =  true;
        String condition =  "Name Contains \"uni\"";
        FSM FSManager =  new FSM();

        /* creates zipped file containing selected files */ 
        FSManager.Zip(directory,recursive,condition,destinationDirectory,zipFileName,overWrite);

        Console.WriteLine("Involved Files: {0} - Affected Files: {1} ",
        FSManager.InvolvedFiles,
        FSManager.AffectedFiles);

        foreach(FileInfo file in FSManager.SelectedFiles)
        {
            Console.WriteLine("{0} - {1} - {2} - {3} - {4}  Bytes",
            file.DirectoryName,
            file.Name,
            file.Extension,
            file.CreationTime,
            file.Length);
        }
    }
}

Here is an example for unzipping:

class Program
{
    static void Main(string[] args)
    {
        String destinationDiretory =  @"c:\zip_files";
        String zipFileName =  "container.zip";        
        Boolean unZipWithDirectoryStructure =  true;
        FSM FSManager =  new FSM();

        /* Unzips files with or without their directory structure */ 
        FSManager.Unzip(zipFileName,
                  destinationDirectory,
                  unZipWithDirectoryStructure);

      }
}

Hope it helps.

I use the dll fileselectionmanager to compress and decompress files and folders, it has worked properly in my project. You can see example in your web http://www.fileselectionmanager.com/#Zipping and Unzipping files and documentation http://www.fileselectionmanager.com/file_selection_manager_documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM