c# copy zip file from embeded resorce to disk issue

mion shion 241 Reputation points
2022-11-11T04:25:22.1+00:00

Good morning all,

ok so i been trying my best to get this to work but for the life of me keeps returning null.

code i have -
public void exactzip(string name)
{
try
{

                //ExtractSaveResource("ffmpeg", ffmpegpathset);  
  
                if (!Directory.Exists(name + "\\ffmpeg"))  
                {  
  
                    byte[] myfile = Resources.ffmpeg;  
                    File.WriteAllBytes("ffmpeg.zip", myfile);  
                    bool flag = File.Exists("ffmpeg.zip");  
                    if (flag)  
                    {  
  
  
                        if(ffmpegpathset != string.Empty)  
                        {  
                            using (ZipFile zip = ZipFile.Read(ffmpegpathset + "ffmpeg.zip"))  
                            {  
                                foreach (ZipEntry e in zip)  
                                {  
                                    e.Extract(ffmpegpathset, ExtractExistingFileAction.OverwriteSilently);  
                                }  
                            }  
                        }  
                        else  
                        {  
                            using (ZipFile zip = ZipFile.Read(ffmpegpathset + "//ffmpeg.zip"))  
                            {  
                                foreach (ZipEntry e in zip)  
                                {  
                                    e.Extract(ffmpegpathset, ExtractExistingFileAction.OverwriteSilently);  
                                }  
                            }  
                        }  
  
  
                    }  
                }  
                else  
                {  
  
                }  
            }  

and i have checked the resorces file

that has the following,
public static byte[] ffmpeg {
get {
object obj = ResourceManager.GetObject("ffmpeg", resourceCulture);
return ((byte[])(obj));
}
}

but issue i have is it goes though but the file write bytes does not copy the file to the c drive for it to be extracted i have been using this iocic.zip for ages for all my unzipping

but this is not working for what ever reason and i cant seem to understand why this is

Kind regards,
elfenliedtopfan5

Developer technologies Windows Forms
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2022-11-11T09:44:12.05+00:00

    Is this not because you're reading & writing to different locations?

    Here you're referring to ffmpeg.zip in your current directory:

       File.WriteAllBytes("ffmpeg.zip", myfile);  
       bool flag = File.Exists("ffmpeg.zip");  
    

    But you're reading it from the ffmpegpathset path:

       using (ZipFile zip = ZipFile.Read(ffmpegpathset + "//ffmpeg.zip"))  
    
    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-11-11T11:25:59.773+00:00

    See my GitHub repository, below is one example that should work.

    /// <summary>  
    /// Decompress files in .zip file  
    /// </summary>  
    /// <param name="fileName">Zip file</param>  
    /// <param name="baseFolder">Location to unzip which will have a datetime appended</param>  
    public async Task DecompressTask(string fileName, string baseFolder)  
    {  
        ZipEntry currentEntry = new ZipEntry();  
      
        string extractPath = Path.Combine(baseFolder, $"{DateTime.Now:HH-mm-ss-fff}");  
      
        Directory.CreateDirectory(extractPath);  
      
        if (Directory.Exists(extractPath))  
        {  
            if (Directory.Exists(extractPath))  
            {  
                Directory.Delete(extractPath, true);  
            }  
        }  
      
        await Task.Run(async () =>  
        {  
      
            using (var zip = ZipFile.Read(fileName))  
            {  
                // subscribers get notification of each file extracted  
                zip.ExtractProgress += ZipOnExtractProgress;  
      
                var selection = from zipEntry in zip.Entries select zipEntry;  
      
                try  
                {  
                    foreach (ZipEntry entry in selection)  
                    {  
                        currentEntry = entry;  
                        entry.Extract(extractPath);  
                    }  
                }  
                catch (Exception ex)  
                {  
                    // TODO  
                }  
            }  
      
            await Task.Delay(50);  
      
        });  
    }  
      
    private void ZipOnExtractProgress(object sender, ExtractProgressEventArgs e)  
    {  
        SendProgressUpdate?.Invoke(sender, e);  
    }  
      
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.