C#如何在没有原始文件路径的情况下将图像,音频,视频添加到dotnetzip zip文件中? [英] C# how do I add image, audio, video to dotnetzip zip file without the original filepath?

查看:188
本文介绍了C#如何在没有原始文件路径的情况下将图像,音频,视频添加到dotnetzip zip文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C#.NET WinForms应用程序,我需要创建一个包含以下类型项目的zip文件(使用DotNetZip):



系统.Drawing.Image someimage

Microsoft.DirectX.AudioVideoPlayback.Audio someaudio

Microsoft.DirectX.AudioVideoPlayback.Video somevideo



问题:在我发现的所有代码示例中,将图像,音频或视频文件转换为字节数组或Base64字符串,使用文件的原始文件路径转换项目......



I'm writing a C# .NET WinForms app where I need to create a zip file (using DotNetZip) consisting of items of the following types:

System.Drawing.Image someimage
Microsoft.DirectX.AudioVideoPlayback.Audio someaudio
Microsoft.DirectX.AudioVideoPlayback.Video somevideo

Problem: In all the code examples I've found in which an image, audio or video file is converted to a byte array or Base64 string, the items are converted using the files' original filepaths...

byte() MediaInBytes = File.ReadAllBytes(originalfilepath);





由于生成的zip文件可能在其创建的计算机以外的其他计算机上打开,我无法使用每个图像,音频或视频文件的原始文件filepath,因为那些图像,音频或视频文件可能不存在于另一台计算机上。一旦我添加了图像,音频或视频文件,就不再使用原始文件路径。



所以我有两种选择:



1.找到一种方法将someimage,someaudio和somevideo添加到用DotNetZip创建的zip存档中,而不是先将它们转换为Base64字符串或字节数组,或者

2.找到一种方法将someimage,someaudio和somevideo的值写入临时文件,以便我可以从那里添加它们。请记住,要将项目添加到DotNetZip zip文件中,该项目必须来自文件路径:





Since the resulting zip file may be opened on another computer other than the one it was created on, I cannot use each image, audio or video file's original filepath because those image, audio or video files may not exist on another computer. Once I add an image, audio or video file, the original filepath should no longer be used.

So I'm left with two options:

1. Find a way to add someimage, someaudio and somevideo to a zip archive created with DotNetZip without converting them to Base64 string or byte array first, or
2. Find a way to write the values of someimage, someaudio and somevideo to a temp file so I can add them from there. Remember, to add an item to a DotNetZip zip file, the item must originate from a filepath:

Ionic.Zip.ZipEntry someEntry = zip.AddFile(filepath);





我们将非常感谢任何帮助。



我尝试过:





Any help will be greatly appreciated.

What I have tried:

byte() MediaInBytes = File.ReadAllBytes(someimage);




byte() MediaInBytes = File.ReadAllBytes(someaudio);




byte() MediaInBytes = File.ReadAllBytes(somevideo);




Ionic.Zip.ZipEntry someEntry = zip.AddFile(filepath);

推荐答案

嗯......似乎你不知道.ZIP文件是如何工作的。或者你在描述这个问题时做得很糟糕。



原始路径没有保存在.ZIP中。如果将文件夹添加到.ZIP,则会保存RELATIVE路径。这意味着文件解压缩到相对于解压缩文件的人希望它们出现的位置的路径,而不是原始文件来自的绝对路径。



你似乎比实际上要困难得多。
Ummm...it seems you have no idea how .ZIP files work. Either that or you're doing a terrible job of describing the problem.

The original path is not saved in the .ZIP. If you add folders to a .ZIP the RELATIVE path is saved. That means the files are unzipped to a path relative to where the person unzipping the file wants them to appear, NOT to the absolute path the original files came from.

You seem to be making this much harder than it actually is.


我不知道DotNetZip,但7zip可以做到,你可以用Process.Start()来调用它。 />
来自7zip帮助的示例:

I don't know about DotNetZip, but 7zip can do it, you can call it with Process.Start().
Examples from 7zip help:
7z a archive1.zip subdir\

从文件夹子目录添加所有文件和子文件夹归档archive1.zip。存档中的文件名将包含subdir \前缀。

adds all files and subfolders from folder subdir to archive archive1.zip. The filenames in archive will contain subdir\ prefix.

7z a archive2.zip .\subdir\*

将文件夹子目录中的所有文件和子文件夹添加到存档archive2。压缩。存档中的文件名不包含subdir \前缀。

adds all files and subfolders from folder subdir to archive archive2.zip. The filenames in archive will not contain subdir\ prefix.

cd /D c:\dir1\
7z a c:\archive3.zip dir2\dir3\

存档c:\ archive3.zip中的文件名将包含dir2 \dir3 \前缀,但它们不包含c:\dir1 \前缀。

- -------------------------------------------------- -----------------

关于文件路径

处理文件时你不能当文件与.exe文件位于同一目录时,需要文件路径,例如:

The filenames in archive c:\archive3.zip will contain dir2\dir3\ prefix, but they will not contain c:\dir1\ prefix.
---------------------------------------------------------------------
About file paths
When working with files you don't need a file path when the file is in the same directory as the .exe file, example:

string text1 = File.ReadAllText("file.txt");


Dave,

我知道原始文件路径没有保存在.zip中,只保存在它的末尾。显然,DotNetZip只允许我从文件路径添加一个文件,所以我需要找到一种方法将变量的值写入临时文件,这样我就可以从该临时文件中读取一个字节数组,然后添加字节数组到.zip。
Dave,
I know that the original filepath is not saved in the .zip, only what's at the end of it. Apparently, DotNetZip will only allow me to add a file from a filepath, so I need to find a way to write the value of a variable to a temp file so I can read it into a byte array from that temp file and then add the byte array to the .zip.


这篇关于C#如何在没有原始文件路径的情况下将图像,音频,视频添加到dotnetzip zip文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆