简体   繁体   English

将文件从一个 Zip 文件复制到另一个

[英]Copy files from one Zip file to another

I am copying files from one zip file to another in certain circumstances.在某些情况下,我正在将文件从一个 zip 文件复制到另一个文件。 I am wondering if there is a better way to do it than what I came up with:我想知道是否有比我想出的更好的方法:

using FileStream sourceFileStream = new FileStream(source.FileName, FileMode.Open);
using FileStream targetFileStream = new FileStream(target.FileName, FileMode.Open, FileAccess.ReadWrite);
using ZipArchive sourceZip = new ZipArchive(sourceFileStream, ZipArchiveMode.Read);
using ZipArchive targetZip = new ZipArchive(targetFileStream, ZipArchiveMode.Update);
ZipArchiveEntry sourceEntry = sourceZip.GetEntry(filePathInArchive);
if (sourceEntry == null) 
    return;
ZipArchiveEntry targetEntry = targetZip.GetEntry(filePathInArchive);
if (targetEntry != null) 
    targetEntry.Delete();
targetZip.CreateEntry(filePathInArchive);
targetEntry = targetZip.GetEntry(filePathInArchive);
if (targetEntry != null)
{
    Stream writer = targetEntry.Open();
    Stream reader = sourceEntry.Open();

    int b;
    do
    {
        b = reader.ReadByte();
        writer.WriteByte((byte)b);
    } while (b != -1);


    writer.Close();
    reader.Close();
}

Tips and suggestions would be appreciated.提示和建议将不胜感激。

You can iterate each entry from source archive with opening its streams and using Stream.CopyTo write source entry content to target entry.您可以通过打开其流并使用Stream.CopyTo将源条目内容写入目标条目来迭代源存档中的每个条目。

From C# 8.0 it looks compact and works fine:C# 8.0开始,它看起来很紧凑并且工作正常:

static void CopyZipEntries(string sourceZipFile, string targetZipFile)
{
    using FileStream sourceFS = new FileStream(sourceZipFile, FileMode.Open);
    using FileStream targetFS = new FileStream(targetZipFile, FileMode.Open);

    using ZipArchive sourceZIP = new ZipArchive(sourceFS, ZipArchiveMode.Read, false, Encoding.GetEncoding(1251));
    using ZipArchive targetZIP = new ZipArchive(targetFS, ZipArchiveMode.Update, false, Encoding.GetEncoding(1251));

    foreach (ZipArchiveEntry sourceEntry in sourceZIP.Entries)
    {
        // 'is' is replacement for 'null' check
        if (targetZIP.GetEntry(sourceEntry.FullName) is ZipArchiveEntry existingTargetEntry)
            existingTargetEntry.Delete();

        using (Stream targetEntryStream = targetZIP.CreateEntry(sourceEntry.FullName).Open())
        {
            sourceEntry.Open().CopyTo(targetEntryStream);
        }
    }
}

With earlier than C# 8.0 versions it works fine too, but more braces needed:对于C# 8.0之前的版本,它也可以正常工作,但需要更多的括号:

static void CopyZipEntries(string sourceZipFile, string targetZipFile)
{
    using (FileStream sourceFS = new FileStream(sourceZipFile, FileMode.Open))
    {
        using (FileStream targetFS = new FileStream(targetZipFile, FileMode.Open))
        {
            using (ZipArchive sourceZIP = new ZipArchive(sourceFS, ZipArchiveMode.Read, false, Encoding.GetEncoding(1251)))
            {
                using (ZipArchive targetZIP = new ZipArchive(targetFS, ZipArchiveMode.Update, false, Encoding.GetEncoding(1251)))
                {
                    foreach (ZipArchiveEntry sourceEntry in sourceZIP.Entries)
                    {
                        if (targetZIP.GetEntry(sourceEntry.FullName) is ZipArchiveEntry existingTargetEntry)
                        {
                            existingTargetEntry.Delete();
                        }

                        using (Stream target = targetZIP.CreateEntry(sourceEntry.FullName).Open())
                        {
                            sourceEntry.Open().CopyTo(target);
                        }
                    }
                }
            }
        }
    }
}

For single specified file copy just replace bottom part from foreach loop to if condition:对于单个指定的文件副本,只需将foreach循环的底部部分替换为 if 条件:

static void CopyZipEntry(string fileName, string sourceZipFile, string targetZipFile)
{
    // ...

    // It means specified file exists in source ZIP-archive
    // and we can copy it to target ZIP-archive
    if (sourceZIP.GetEntry(fileName) is ZipArchiveEntry sourceEntry) 
    {
        if (targetZIP.GetEntry(sourceEntry.FullName) is ZipArchiveEntry existingTargetEntry)
            existingTargetEntry.Delete();

        using (Stream targetEntryStream = targetZIP.CreateEntry(sourceEntry.FullName).Open())
        {
            sourceEntry.Open().CopyTo(targetEntryStream);
        }
    }
    else
        MessageBox.Show("Source ZIP-archive doesn't contains file " + fileName);
}

Thanks to the input so far, I cleaned up and improved the code.感谢到目前为止的输入,我清理并改进了代码。 I think this looks cleaner and more reliable.我认为这看起来更干净、更可靠。

//Making sure files exist etc before this part...
string filePathInArchive = source.GetFilePath(fileId);

using FileStream sourceFileStream = new FileStream(source.FileName, FileMode.Open);
using FileStream targetFileStream = new FileStream(target.FileName, FileMode.Open, FileAccess.ReadWrite);
using ZipArchive sourceZip = new ZipArchive(sourceFileStream, ZipArchiveMode.Read, false );
using ZipArchive targetZip = new ZipArchive(targetFileStream, ZipArchiveMode.Update, false);

ZipArchiveEntry sourceEntry = sourceZip.GetEntry(filePathInArchive);

if (sourceEntry != null)
{
    if (targetZip.GetEntry(filePathInArchive) is { } existingTargetEntry)
    {
        existingTargetEntry.Delete();
    }

    using var targetEntryStream = targetZip.CreateEntry(sourceEntry.FullName).Open();
    sourceEntry.Open().CopyTo(targetEntryStream);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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