Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文件直接移动到zip文件中_C# - Fatal编程技术网

C# 将文件直接移动到zip文件中

C# 将文件直接移动到zip文件中,c#,C#,我正在尝试直接将源文件.mp4移动到目标文件.Zip。我目前正在尝试创建一个新条目Transfer.mp4,并在上面复制字节。代码一直运行到最后,但该文件从未添加到ZIP文件中。我不确定我是否遗漏了什么或者这是不可能的 string sourceFile = basePath + "Source_File.mp4"; string destinationZip = basePath + "Desintation_Zip.zip"; using (var file = File.OpenRead

我正在尝试直接将
源文件.mp4
移动到
目标文件.Zip
。我目前正在尝试创建一个新条目
Transfer.mp4
,并在上面复制字节。代码一直运行到最后,但该文件从未添加到ZIP文件中。我不确定我是否遗漏了什么或者这是不可能的

string sourceFile = basePath + "Source_File.mp4";
string destinationZip = basePath + "Desintation_Zip.zip";

using (var file = File.OpenRead(destinationZip))
{
    MemoryStream memoryStream = new MemoryStream();
    memoryStream.SetLength(file.Length);
    file.Read(memoryStream.GetBuffer(), 0, (int)file.Length);

    using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Update))
    {
        var entry = zip.CreateEntry("Transfer.mp4");
        using (var destinationStream = entry.Open())
        using (var sourceStream = File.OpenRead(sourceFile))
        {
            sourceStream.CopyTo(destinationStream);
        }
    }
}

我仍然不确定为什么我的初始代码不起作用,但我通过这样做使它起作用:

string sourceFile = basePath + "Source_File.mp4";
string destinationZip = basePath + "Desintation_Zip.zip";

using (var destinationZipFileStream = new FileStream(destinationZip, FileMode.Open))
using (var destinationZipArchive = new ZipArchive(destinationZipFileStream, ZipArchiveMode.Update))
{
    ZipArchiveEntry destinationWriter = destinationZipArchive.CreateEntry("Transfer.mp4");
    using (var writer = new StreamWriter(destinationWriter.Open()))
    using (var sourceStream = File.OpenRead(sourceFile))
    {
        sourceStream.CopyTo(writer.BaseStream);
    }
}   

注意:您需要再次检查以确保该文件不存在,否则您将以这种方式复制文件。

我仍然不确定我的初始代码为什么不起作用,但我通过这样做使其起作用:

string sourceFile = basePath + "Source_File.mp4";
string destinationZip = basePath + "Desintation_Zip.zip";

using (var destinationZipFileStream = new FileStream(destinationZip, FileMode.Open))
using (var destinationZipArchive = new ZipArchive(destinationZipFileStream, ZipArchiveMode.Update))
{
    ZipArchiveEntry destinationWriter = destinationZipArchive.CreateEntry("Transfer.mp4");
    using (var writer = new StreamWriter(destinationWriter.Open()))
    using (var sourceStream = File.OpenRead(sourceFile))
    {
        sourceStream.CopyTo(writer.BaseStream);
    }
}   

注意:您需要再次检查以确保该文件不存在,否则您将以这种方式复制文件。

我的猜测是,即使您已阅读并更改了该文件,您也没有写回任何内容,即更改保留到
MemoryStream
,并且此后不会再进行任何更改

您可以试试这个(假设您使用的是
System.IO.Compression.ZipFile
):

或者,如果您使用的是
ICSharpCode.SharpZipLib.Zip.ZipFile
,请执行以下操作:

using (var fileStream = new FileStream(destinationZip, FileMode.Open))
using (var zip = new ZipArchive(fileStream, ZipArchiveMode.Update))
{
    var entry = zip.CreateEntry("Transfer.mp4");
    using (var destinationStream = entry.Open())
    using (var sourceStream = File.OpenRead(sourceFile))
    {
        sourceStream.CopyTo(destinationStream);
    }
}

我的猜测是,即使您阅读并修改了该文件,您也没有写任何内容,即更改一直保存到
MemoryStream
,并且在这之后不会再进行任何更改

您可以试试这个(假设您使用的是
System.IO.Compression.ZipFile
):

或者,如果您使用的是
ICSharpCode.SharpZipLib.Zip.ZipFile
,请执行以下操作:

using (var fileStream = new FileStream(destinationZip, FileMode.Open))
using (var zip = new ZipArchive(fileStream, ZipArchiveMode.Update))
{
    var entry = zip.CreateEntry("Transfer.mp4");
    using (var destinationStream = entry.Open())
    using (var sourceStream = File.OpenRead(sourceFile))
    {
        sourceStream.CopyTo(destinationStream);
    }
}

啊,OP skeeted:)啊,OP skeeted:)原因正如@kalexi所指出的。您正在以
MemoryStream
的形式读取归档文件,将该流表示为
ZipArchive
并添加到归档文件中,但您实际将
MemoryStream
保存回磁盘上的文件的何处?依赖本机Zip API将处理读取和持久化更改。原因正如@kalexi所指出的。您正在以
MemoryStream
的形式读取归档文件,将该流表示为
ZipArchive
并添加到归档文件中,但您实际将
MemoryStream
保存回磁盘上的文件的何处?依赖本机Zip API将处理读取和持久化更改。当我尝试导入
ZipFile
的库时,它表示没有
Open
的定义。您使用的是
System.IO.Compression.ZipFile
?如果您使用的是
ICSharpCode.SharpZipLib.Zip.ZipFile
,这可能就是原因。给我几秒钟,我应该能够很快地更新答案。当我尝试导入
ZipFile
的库时,它说没有
Open
的定义。您是否使用
System.IO.Compression.ZipFile
?如果您使用的是
ICSharpCode.SharpZipLib.Zip.ZipFile
,这可能就是原因。给我几秒钟,我应该能很快更新答案。