如何将文件从一个ZipArchive复制到另一个

时间:2019-04-03 16:16:15

标签: c# file copy ziparchive

我希望能够将文件从现有的zip文件复制到新的zip文件。

据我所知,有两种方法可以做到这一点,但没有一种可行。

我可以使用

archive.CreateEntryFromFile(file.FullPath, file.Name + "." + file.Extention);

但是,文件名是“ C:\ For Attachment \ atest3_1.zip \ BR_FEE_VerifyFeePackContent_S.sql”,这会导致以下错误:

找不到路径'C:\ For Attachment \ atest3_1.zip \ BR_FEE_VerifyFeePackContent_S.sql'的一部分。

因此,也许我需要提取zip存档中文件的内容,然后使用以下代码将其写入新的存档中:

zipItem = archive.CreateEntry(file.Name + "." + file.Extention);                           
using (Stream sourceStream = file.MemoryStream)
{
    using (Stream destinationStream = zipItem.Open())
    {
        sourceStream.CopyTo(destinationStream);
    }
}

这将创建文件而不会引发任何错误,但是文件为空-内容实际上并未移动。

整个方法如下:

    private void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string fileName = "";
            if (FormatFileName(out fileName))
            {
                SourceFile file;
                ZipArchiveEntry zipItem;
                using (ZipArchive archive = ZipFile.Open(fileName, ZipArchiveMode.Create))
                {
                    for (int i = 0; i < lstIncludedFiles.Items.Count; i++)
                    {
                        file = new SourceFile(lstIncludedFiles.Items[i].ToString());

                        zipItem = archive.CreateEntry(file.Name + "." + file.Extention);                           
                        using (Stream sourceStream = file.MemoryStream)
                        {
                            using (Stream destinationStream = zipItem.Open())
                            {
                                //destinationStream.Flush();
                                sourceStream.CopyTo(destinationStream);
                            }
                        }



                        //archive.CreateEntryFromFile(file.FullPath, file.Name + "." + file.Extention);
                    }
                }
                Clipboard.SetText(fileName);
                MessageBox.Show(this, fileName + " was created and the name copied to your clipboard.", "Operation Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        catch (Exception ex)
        {
            ShowError(ex);
        }
    }
    private void ShowError(Exception ex)
    {
        MessageBox.Show(this, ex.Message, "An error has occured", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }

以及源文件类:

public class SourceFile
{
    private string _name;
    private string _fullPath;
    private string _extention;
    private MemoryStream _memoryStream;
    private Byte[] _fileBytes;

    public SourceFile(string fullPath)
    {
        _fullPath = fullPath.Replace(" \\ ", "\\");
        _name = _fullPath.Split('\\')[_fullPath.Split('\\').Length - 1];
        _extention = _name.Split('.')[_name.Split('.').Length - 1];
        _name = _name.Replace("." + _extention, "");
        FileStream fs;
        _memoryStream = new MemoryStream();
        if (_fullPath.Contains(".zip\\"))
        {

            string zipFilePath = _fullPath.Substring(0, _fullPath.IndexOf(".zip\\") + 4);
            ZipArchive zipFile = ZipFile.OpenRead(zipFilePath);
            ZipArchiveEntry file = null;
            for (int i = 0; ((file == null) && (i < zipFile.Entries.Count)); i++)
            {
                if (zipFile.Entries[i].FullName == _fullPath.Split('\\')[_fullPath.Split('\\').Length - 1])
                {
                    file = zipFile.Entries[i];
                }
            }               
            file.Open().CopyTo(_memoryStream);               

        }
        else
        {
            fs = new FileStream(_fullPath, FileMode.Open, FileAccess.Read);
            fs.CopyTo(_memoryStream);
            /*
            br = new BinaryReader(fs);
            long numBytes = new FileInfo(_fullPath).Length;
            _fileBytes = br.ReadBytes((int)numBytes);
            _memoryStream = new MemoryStream(_fileBytes);
            */
        }            

    }
    public string FullPath
    {
        get { return _fullPath; }
    }
    public string Name
    {
        get { return _name; }
    }

    public string Extention
    {
        get { return _extention; }
    }

    public Byte[] FileBytes
    {
        get { return _fileBytes; }
    }

    public MemoryStream MemoryStream
    {
        get { return _memoryStream; }
    }
}

0 个答案:

没有答案