Подтвердить что ты не робот

.Net Zip Up файлы

Каков наилучший способ закрепить файлы с помощью С#? В идеале я хочу иметь возможность разделять файлы в один архив.

4b9b3361

Ответ 1

Вы можете использовать DotNetZip для архивирования этого. Он может использоваться в любом приложении.

Вот пример кода:

   try
   {
     // for easy disposal
     using (ZipFile zip = new ZipFile())
     {
       // add this map file into the "images" directory in the zip archive
       zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
       // add the report into a different directory in the archive
       zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
       zip.AddFile("ReadMe.txt");
       zip.Save("MyZipFile.zip");
     }
   }
   catch (System.Exception ex1)
   {
     System.Console.Error.WriteLine("exception: " + ex1);
   }

Ответ 3

Вы посмотрели SharpZipLib?

Я считаю, что вы можете создавать zip файлы с классами в пространстве имен System.IO.Packaging - но каждый раз, когда я пытался заглянуть в него, Я нашел это довольно запутанным...

Ответ 4

Взгляните на эту библиотеку: http://www.icsharpcode.net/OpenSource/SharpZipLib/

Он довольно всеобъемлющий, он имеет дело со многими форматами, является открытым исходным кодом, и вы можете использовать его в коммерческих приложениях с закрытым исходным кодом.

Это очень просто использовать:

byte[] data1 = new byte[...];
byte[] data2 = new byte[...];
/*...*/

var path = @"c:\test.zip";
var zip = new ZipOutputStream(new FileStream(path, FileMode.Create))
            {
                IsStreamOwner = true
            }

zip.PutNextEntry("File1.txt");
zip.Write(data1, 0, data1.Length);

zip.PutNextEntry("File2.txt");
zip.Write(data2, 0, data2.Length);

zip.Close();
zip.Dispose();

Ответ 5

Существует несколько библиотек: наиболее популярными из них являются DotNetZip и SharpZipLib.

Ответ 6

Привет, я создал два метода в библиотеке ShapLib (вы можете скачать его здесь http://www.icsharpcode.net/opensource/sharpziplib/), которые хотели бы поделиться, они очень простой в использовании только проходной источник и целевой путь (полный путь, включая папку/файл и расширение). Надеюсь, это поможет вам!

//ALLYOURNAMESPACESHERE
using ...

//SHARPLIB
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

public static class FileUtils
{

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sourcePath"></param>
    /// <param name="targetPath"></param>
    public static void ZipFile(string sourcePath, string targetPath)
    {
        string tempZipFilePath = targetPath;
        using (FileStream tempFileStream = File.Create(tempZipFilePath, 1024))
        {
            using (ZipOutputStream zipOutput = new ZipOutputStream(tempFileStream))
            {
                // Zip with highest compression.
                zipOutput.SetLevel(9);
                DirectoryInfo directory = new DirectoryInfo(sourcePath);
                foreach (System.IO.FileInfo file in directory.GetFiles())
                {

                    // Get local path and create stream to it.
                    String localFilename = file.FullName;
                    //ignore directories or folders
                    //ignore Thumbs.db file since this probably will throw an exception
                    //another process could be using it. e.g: Explorer.exe windows process
                    if (!file.Name.Contains("Thumbs") && !Directory.Exists(localFilename))
                    {
                        using (FileStream fileStream = new FileStream(localFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            // Read full stream to in-memory buffer.
                            byte[] buffer = new byte[fileStream.Length];
                            fileStream.Read(buffer, 0, buffer.Length);

                            // Create a new entry for the current file.
                            ZipEntry entry = new ZipEntry(file.Name);
                            entry.DateTime = DateTime.Now;

                            // set Size and the crc, because the information
                            // about the size and crc should be stored in the header
                            // if it is not set it is automatically written in the footer.
                            // (in this case size == crc == -1 in the header)
                            // Some ZIP programs have problems with zip files that don't store
                            // the size and crc in the header.
                            entry.Size = fileStream.Length;
                            fileStream.Close();

                            // Update entry and write to zip stream.
                            zipOutput.PutNextEntry(entry);
                            zipOutput.Write(buffer, 0, buffer.Length);

                            // Get rid of the buffer, because this
                            // is a huge impact on the memory usage.
                            buffer = null;
                        }
                    }
                }

                // Finalize the zip output.
                zipOutput.Finish();

                // Flushes the create and close.
                zipOutput.Flush();
                zipOutput.Close();
            }
        }
    }

    public static void unZipFile(string sourcePath, string targetPath)
    {
        if (!Directory.Exists(targetPath))
            Directory.CreateDirectory(targetPath);

        using (ZipInputStream s = new ZipInputStream(File.OpenRead(sourcePath)))
        {

            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {

                if (theEntry.Name != String.Empty)
                {
                    using (FileStream streamWriter = File.Create(targetPath + "\\" + theEntry.Name))
                    {

                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

}