DotNetZip uncompressed exe files not opening - c#

I'v been using the DotNetZip library for uncompressing some files from a ZIP file, compressed using the same library. That ZIP file contains exe files, but once decompressed, some of the exe file decompressed is not opening anymore. That problem is not happening if i decompress the file manually with WinRAR.
The code i'm using is this one...
if (!System.IO.File.Exists("ApplicationSample.zip")) { MessageBox.Show("No data found", "Warning"); return false; }
var Archive = ZipFile.Open(DatFilePath, ZipArchiveMode.Read);
var InstallPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + #"/ApplicationSample";
if (Directory.Exists(InstallPath))
Directory.Delete(InstallPath, true);
Directory.CreateDirectory(InstallPath);
Archive.ExtractToDirectory(InstallPath);
var ArchiveEd = new Ionic.Zip.ZipFile("ApplicationSample.zip");
ArchiveEd.UseUnicodeAsNecessary = true;
ArchiveEd.ZipError += ApplicationSample_ZipError;
ArchiveEd.ExtractAll(InstallPath);
ArchiveEd.Dispose();
ShellLink link = new ShellLink();
link.WorkingDirectory = InstallPath;
link.Target = InstallPath + "/ApplicationSample.exe";
link.IconPath = InstallPath + "/ApplicationSample.ico";
link.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/ApplicationSample.lnk");
The exe file i'm trying to open is an WPF application that works correctly if i do the same operation manually.
I don't know what's happening here and i would like to know what's the problem.
Thanks!!

Related

Blazor Server-Side download issues in IIS

I am trying to download a List of files (user has the option to select multiple). My current code below works fine in localhost (writing and opening the downloads folder). However, when I upload to IIS it gives an error saying that the system configuration is not found.
Please see below:
if (SelectDownloadFiles.Count > 0)
{
//Downloads folder (User profile)
string DownloadFolder = Environment.ExpandEnvironmentVariables("%userprofile%/downloads/");
//This is a little hack to get the literal path for the Downloads folder without too much of back-and-forth and ellaboration
string FolderForward = DownloadFolder.Replace(#"/", #"\");
string Folder = FolderForward.Replace(#"\\", #"\");
foreach (var items in SelectDownloadFiles)
{
//Get Date
var GetDate = items.Substring(0, 6);
//Add 2 days to be consistent to what is displayed to the user (when files were generated)
var FileDate = DateTime.ParseExact(GetDate, "yyMMdd", CultureInfo.InvariantCulture).AddDays(2);
//Get Files
string Pathname = #"D:\";
string FullPathName = Path.Combine(Pathname, items);
byte[] FileBytes = System.IO.File.ReadAllBytes(FullPathName);
MemoryStream Ms = new MemoryStream(FileBytes);
//Rename the file to become user friendly
string DownloadPath = Path.Combine(DownloadFolder, "My Files " + FileDate.ToString("MM-dd-yyyy") + ".zip");
//Write file(s) to folder
FileStream File = new FileStream(DownloadPath, FileMode.Create, FileAccess.Write);
Ms.WriteTo(File);
File.Close();
Ms.Close();
}
//Open Downloads Folder with files
Process.Start("explorer.exe", Folder);
navigationManager.NavigateTo("/default", true);
//DisplayMessage.Show("File(s) successfully downloaded. Please check your “Downloads” folder to access your file(s).", "OK", "check");
}
else
{
Toaster.Add("Please select at least one file to download.", MatToastType.Warning);
}
I've also tried to use the solution below to no avail:
private readonly IWebHostEnvironment _webHostEnvironment;
public YourController (IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment= webHostEnvironment;
}
If I use the "folderoptionpath" and choose "MyDocuments" for instance, the files download to the root path of the files inside IIS.
Is there anything else I need to be doing to get to this to work?
Thanks in advance!
Well, after spending some time researching this, I was finally able to get it going. I ended up using a Nuget Package called BlazorFileSaver and it works just fine. Here's the repo: https://github.com/IvanJosipovic/BlazorFileSaver/blob/master/src/BlazorFileSaver.Sample/Pages/Index.razor
I hope this can help someone else in the future.

Zip files in a folder using c# and return response

I want to zip files and folders in a folder using c# and I've checked previously asked questions but they don't work for me...i'm currently trying to work with DotNetZip. Here's a bit of my code:
using (ZipFile zip = new ZipFile())
{
string[] files = Directory.GetFiles(#"C:\Users\T1132\Desktop\logs");
// add all those files to the logs folder in the zip file
zip.AddFiles(files, "logs");
zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
var a = System.DateTime.Now.ToString();
zip.Save(#"C:\Users\T1132\Desktop\logs\Archiver.zip");
foreach (var f in files)
{
System.IO.File.Delete(f);
System.Diagnostics.Debug.WriteLine(f + "will be deleted");
}
}
the code above works but it only zips the files and leaves the folders. Kindly assist me please, Thanks.
There are so many ways to zip files using DotNetZip.
using (ZipFile zip = new ZipFile())
{
zip.Encoding = System.Text.Encoding.GetEncoding("big5"); // chinese
zip.AddDirectory(#"MyDocuments\ProjectX");
zip.Save(ZipFileToCreate);
}
Above zips a directory. For more uses you can follow the links
https://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples
or
http://grapevine.dyndns-ip.com/download/authentec/download/dotnetzip/examples.htm
Thanks
I am using this and it is working great. Make sure directory has enough permission(s).
System.IO.Compression.ZipFile.CreateFromDirectory(zipPath, path + strFileName + ".zip");
You can use below code.
string zipFileName= "zip full path with extension";
var files = Directory.GetFiles(" directory path");
Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile();
foreach(var file in files) {
zip.AddFile(file);
}
zip.Save(zipFileName);

Download File from DataTable

I'm trying to develop some kind of FTP program in school. The service they provided me to download files from their server only downloads one file at a time and if I try to download a folder it returns a blank file. How is it possible to create some kind of system to download the whole directory (even the files inside the directory of the selected directory)?
This is what I use to get the files:
foreach (ListViewItem cadaItem in listView2.SelectedItems)
{
string type = listView2.SelectedItems[0].SubItems[2].Text;
MessageBox.Show(type);
byte[] fileBytes = new byte[666666];
byte[] hash = new byte[666666];
if (type != "")
{
servDin.GetFileMD5Async("", "", Vars.pastaLocal + #"\" + cadaItem.Text, hash, fileBytes);
using (Stream file = File.OpenWrite(Vars.pastaLocal + #"\" + Convert.ToString(cadaItem.Text)))
{
file.Write(fileBytes, 0, fileBytes.Length);
}
}
I wanted to use a datatable to get the filenames and download the files from there. is it possible?
Thank You

Attach file to Winform and copy the file to local while running exe c#

Is it possible to attach a text file resource to my Winform exe. So when I run the "Form.exe" in another computer then it copy the text file to a specified folder. Please suggest a method to achieve the same. Thanks
If the resource name is a string:
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream(resourceName))
using (var reader = new StreamReader(stream))
{
string text = reader.ReadToEnd();
File.WriteAllText(fileName, text);
}
else:
File.WriteAllText(fileName, Properties.Resources.TextFile1);
And also make sure that you have set the Build Action of the resource file to "Embedded Resource".
First you need to add your file as a resource in your project.
This explains what to do
Then select your file and in the properties change the "Build Action" to "Embedded Resource". This will now embed your file in your output (.exe).
To extract the file you need to do the following;
String myProject = "Name of your project";
String file = "Name of your file to extract";
String outputPath = #"c:\path\to\your\output";
using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(myProject + ".Resources." + file))
{
using (System.IO.FileStream fileStream = new System.IO.FileStream(outputPath + "\\" + file, System.IO.FileMode.Create))
{
for (int i = 0; i < stream.Length; i++)
{
fileStream.WriteByte((byte)stream.ReadByte());
}
fileStream.Close();
}
}
Ideally you should check that the file does not already exist before you do this. Don't forget also to catch exceptions. Which can be very common when dealing with the file system.
Add the text file to your project resources
Properties -> Resources -> Add Resource
Read the data from the resource using
var text = Properties.Resources.textFile;
Write to the file with
File.WriteAllText(#"C:\test\testOut.txt", text);

Ionic.Zip Splitt up zip can't be extracted

I zip a directory with Ionic.Zip and splitt it up into several files. The result is a bunch of files named myfile.zip, myfile.z01, myfile.z02,...
When I look into the zip-File with windows explorer, I can see the file list.
But when I try to extract the archive with windows explorer, I get a message The same volume can not be used as both the source and destination
When I open the zipfile with 7zip I get the message file myfile.zip cannot be opened as archive
Creating and extracting a single zip-archive works fine.
Here is the code, where I create the zip archive, using Ionic.Zip
using (ZipFile zip = new ZipFile())
{
//zip.AlternateEncoding = System.Text.Encoding.UTF8;
zip.AddDirectory(sourceDirectory);
//zip.MaxOutputSegmentSize = 0; //Single file
zip.MaxOutputSegmentSize = 1024 * 1024 * 8; //Splitt up into 8 MB pieces
//zip.Password = zipPassword;
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.None;
zip.CompressionMethod = CompressionMethod.None;
zip.Save(zipFilePath);
segmentsCreated = zip.NumberOfSegmentsForMostRecentSave;
}
return segmentsCreated;
Btw, I tried several combinations of CompressionLevels, CompressionMethods, with and without password,... No changes :(
UPDATE 1:
Unpacking works:
using (ZipFile zip = ZipFile.Read(zipFilePath))
{
zip.Password = zipPassword;
zip.ExtractAll(targetDirectory, ExtractExistingFileAction.OverwriteSilently);
}
This is a known issue.
you can't open a partial zip file using the windows explorer.
Use WinRar.

Categories