Click here to Skip to main content
15,507,361 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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);

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);
Updated 18-Jun-17 8:39am 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.
Share this answer
I don't know about DotNetZip, but 7zip can do it, you can call it with Process.Start().
Examples from 7zip help:
7z a subdir\
adds all files and subfolders from folder subdir to archive The filenames in archive will contain subdir\ prefix.
7z a .\subdir\*
adds all files and subfolders from folder subdir to archive The filenames in archive will not contain subdir\ prefix.
cd /D c:\dir1\
7z a c:\ dir2\dir3\
The filenames in archive c:\ 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");
Share this answer
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.
Share this answer
Rick, the problem is that the end user might open the zip archive in my app on a different computer where those image, audio and video files may not exist. If they open the zip, those files won't load because they won't exist on another computer. Once I add an image, audio or video to a file in the app, that file exists, but because the user could open the zip on another computer, relying on the original filepaths would be useless. As a result, I must find a way to convert the values of the variables that hold the image, audio or video data to byte arrays before writing them to the zip. That way, they can be easily converted back to image, audio or video. using a temp file.

What I'm essentially asking is: Is it possible to write the value of a variable of type Microsoft.DirectXAudioVideoPlayback.Audio or Microsoft.DirectXAudioVideoPlayback.Video to a temp file so I can convert it to a byte array using the code below:

string TempFilePath = Path.GetTempFileName();

byte[] MediaInBytes = File.ReadAllBytes(TempFilePath);
Share this answer
RickZeeland 17-Jun-17 14:41pm    
You are making things too complex, just place your audio and video files next to your applications .exe, that way you don't have to use the "original filepath".
Dave Kreskowiak 18-Jun-17 14:09pm    
You're also adding your comments to other replies as solutions to your own question. The people you're trying to reply to are not getting notifications that you're replying. Click on the "Have a Question or Comment" button to reply to a solution. That way, people you're replying to get a notification that you posted something.
Rick, if you mean copy the files to the app's executable path, that means I'll still have a filepath rely on. Is it possible to write the value of a DirectX Audio or DirectX Video variable to a file? I read the value into a variable from a filepath, now I want to write the value of that variable to a filepath.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900