使用 C# 压缩文件

发布于 2024-09-19 18:32:55 字数 726 浏览 5 评论 0原文

我想使用 C#.Net 将一个“CSV”文件压缩到 Zip 文件中。下面我编写了一些用于创建 Zip 文件的代码,使用此代码我可以创建 zip 文件,但在创建“Data1.zip”文件后手动提取意味着提取的文件扩展名应该是“.csv”,但它不会出现。

        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();
        }

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();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

給妳壹絲溫柔 2024-09-26 18:32:55

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

这是 gzip 压缩,显然它只压缩一个流,解压缩时采用不带 .gz 的存档名称扩大。我不知道我是否在这里。你不妨尝试一下 MSDN 上的代码,看看是否有效。

我使用 ZipLib 进行 zip 压缩。它还支持Bz2,这是一种很好的压缩算法。

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.

凉城已无爱 2024-09-26 18:32:55

使用 ICSharpCode.SharpZipLib(您可以下载它)并执行以下操作

       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);
        }

希望它有帮助...

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...

小矜持 2024-09-26 18:32:55

使用以下库之一:
http://www.icsharpcode.net/opensource/sharpziplib/
http://dotnetzip.codeplex.com/

我更喜欢#ziplib,但两者都有详细的文档记录并且广泛传播。

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.

熟人话多 2024-09-26 18:32:55

从 .NET Framework 4.5 开始,您可以使用内置的 ZipFile 类(在 System.IO.Compression 命名空间中)。

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();

}

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();

}
沉溺在你眼里的海 2024-09-26 18:32:55

在这里查看 FileSelectionManager 库:www.fileselectionmanager.com

首先,您必须将文件选择管理器 DLL 添加到您的项目

这是一个压缩的示例:

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);
        }
    }
}

这是一个解压缩的示例:

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);

      }
}

希望它有所帮助。

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.

与往事干杯 2024-09-26 18:32:55

我使用dll fileselectionmanager来压缩和解压缩文件和文件夹,它在我的项目中工作正常。您可以在您的网站中查看示例 http://www.fileselectionmanager.com/#Zipping 和解压文件
和文档 http://www.fileselectionmanager.com/file_selection_manager_documentation

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文