C# Create zip, add file but can't open - c#

I'm not a C# developer so my knowledge is limited. However, I'm trying to write a program in C# which exports the Windows security log for certain date and export it as an evtx file.
But then what I have to do is add this evtx file to a zip file. The code below creates the zip file (I can see it) and adds evtx file to the zip (I think because in windows explore the size changes from 0kb to more).
#region "COMPRESS THE EVTX FILE INTO A ZIP FILE: "
// First create a new ZIP archive in the "Achives" folder.
string zipFileName = "Zip-" + getDateTimeStamp(1) + ".zip";
string zipFilePath = System.IO.Path.Combine(zipEvtxDirectoryPath, zipFileName);
ZipArchive zipFile = ZipFile.Open(zipFilePath, ZipArchiveMode.Create);
// Add the evtx file created in program directory to this zip archive.
Console.WriteLine(evtxFilePath);
Console.WriteLine(evtxFileName);
zipFile.CreateEntryFromFile(evtxFilePath, evtxFileName);
//evtxFilePath = "D:\TEST\VS\FilesAndFolders\TestDirectory\Archives\Security-Log-Archive-11-01-2015-04-18-58.evtx"
//evtxFileName = "Security-Log-Archive-11-01-2015-04-18-58.evtx"
//zipFilePath = "D:\TEST\VS\FilesAndFolders\TestDirectory\Archives\Zip-11-01-2015-04-18-58"
#endregion
But when I go try to open the zip file using windows explorer, it gives me an error.
Windows cannot open the folder.
The Compressed (zipped) Folder 'D:\TEST\VS\FilesAndFolders\TestDirectory\Archives\Zip-11-01-2015-04-18-58.zip' is invalid.
Perhaps, the code that I'm using is incomplete? or I missed something?

you have written a piece of code that opens the file and writes.
Once the process of creating the file and writing in it, you should tell the PC to close the file.
Im not exactly sure of the commands in this instance, try something like
zipFile.close;

Related

Accessing Excel File Located at Properties.Resources and keep here template

I have a big Excel file with multi sheets that I am using as a template to write data on and then save as a new file to disk. In debug mode, I read it from disk add data to it and save it in a different location without any problems. However, now I need to creat release to my client, but I want to prevent him from reaching the file Excel... so I tried to add my excel file to the Resources but I cann't reread it becaus when I try to read from resources i get it like list of string and i cann't read the forme of template.
I used
NPOI
for read/write the excel file.
So how can I open it from the Resources folder? Is there a better alternative for including such template files to a solution?
I find this solution and it work with me,
for use file Excel, it must already exist physically, so i saved excel file like temp file and i used this temp file for read my data,
string sPath = System.IO.Path.GetTempFileName();
System.IO.File.WriteAllBytes(sPath, Properties.Resources.data_base);
note: Properties.Resources.data_base this is my Excel file.
at the end I delete this temp file for more security
if (System.IO.File.Exists(sPath ))
{
System.IO.File.Delete(sPath );
}

Add Files to Tar Archive Without Copying Parent File Structure

I'm creating a game in which a user can create custom content. There are two files associated with each custom creation: an .ogg file and a .xml file. Previously, I had a folder that contained all of the associated files, but I'd like to wrap all the associated files within a .tar file instead.
Using the following code, I can create a .tar archive (with the custom extension ".krs"):
FileInfo[] filesInDirectory = folder.GetFiles();
string tarArchiveName = #"C:\Users\me\Desktop\UserData\songs\songName.krs";
using (Stream targetStream = new GZipOutputStream(File.Create(tarArchiveName)))
using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(targetStream, TarBuffer.DefaultBlockFactor))
{
foreach (FileInfo file in filesInDirectory)
{
TarEntry entry = TarEntry.CreateEntryFromFile(file.FullName);
tarArchive.WriteEntry(entry, false);
}
}
This doesn't give any errors, but when I open the .krs file as a .tar using 7zip, the the files are buried underneath ALL the parent directories of the original files that were copied to the archive. For example, the path of the "data.xml" file within the .tar file is "C:\Users\me\Desktop\UserData\songs\songName\data.xml".
I want to open the .tar file and there no top-level directory - just the two files. For example the data.xml file within the .tar archive should be simply "data.xml".
I know this is achieveable because I can do it manually using 7zip. How can I do this using the SharpZipLib library in C#? I found this answer that seems to address my problem, but it's written in Python, a language I have no understanding of.
EDIT: I did some more searching and found this answer. I tried the solution, and it took away everything except the first parent directory of the files (".tar : parentFolder\data.xml"). Is it possible to remove that as well to avoid having to do any digging when I extract these files later?
When testing the answer I posted in my edit, I found that when extracting the files, they come out by themselves and do not come in a folder. This is the answer I was looking for.

Possible bug in ZipArchive with archive mode update?

I ran into a problem with ZipArchiveMode.Update if the zip file contains "directory entries".
I know there is no such thing as directory entry but some tools produce entries with ZipArchiveEntry.Length = 0 and ZipArchiveEntry.Name = "" for directories in the zip file.
The following code now corrupts the zip file:
using (ZipArchive archive = ZipFile.Open(#"D:\TEMP\test.zip", ZipArchiveMode.Update))
{
}
As you can see I do nothing at all except for opening the zip file with ZipArchiveMode.Update and dispose it in the end.
The problem is that the "directory entries" seem to be treated as file entries. So in the output there are new zero byte entries with the directory names.
I still can open the zip file and even extract files per drag&drop. But attempts to extract the zip file result in error messages. Maybe because there are two entries with the same fullname?
My workaround was to avoid ZipArchiveMode.Update and use a temporary MemoryStream. Then iterate over all entries, ignore "directory entries" and only copy the file entries to the stream. This way it worked.
Is this a bug in ZipArchive or are entries for directories just wrong? What if I want to store empty directories in the zip? And as I said: Many tools out there seem to produce such directory entries.
I got the same issue when trying to update a zip file with directory entries, after updating the file the zip file exist (with the new added file) but its corrupted.
What worked for me eventually was adding Nuget reference to DotNetZip and add the file using Ionic.Zip:
using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(parameters.ObjzfPath))
{
zip.AddEntry("newFileEntry", "newFileContent"); // you can use zip.AddFile("newFile.txt") as well
zip.Save();
}

Update zip file based on other zip file C#

So i have a zip file in a directory. This zip file contains different kind of files. When a user clicks on a button in my WPF application, the zip file needs to be updated.
It needs to check files from another zip file in another directory. If a file doesnt excist in the first zip file, it needs to copy that file from the other zip file to the new zip file.
i used the Ionic zip methode for this.
So far i just used the file.copy overwrite = true code.
But when the zip file is 1gb+ it is taking very long because it just replaced the zip file.
does someone know how i can resolve this?
Greetings Thomas
UPDATE:
this is the code i got so far:
private void getlocaldata()
{
string admindata = #"\\networklocation\test.zip";
string localPath = #"C:\finaldata\test.zip";
File.Copy(admindata, localPath, true);
}
You can use the DotNetZip library.
Check the file in the zip :
zip["Readme.txt"] = null;
But anyway you must compress files again for changing zips password. You can find examples for that here.

ASP.net MVC - IO.Compression library not recreating the folder structure in a ZIP file

I have a business scenario where I should allow downloading of the encrypted folders as a ZIP file; meaning that the files within the folder and its subfolders are encrypted. As I perform server side decryption of the files it is necessary first to recreate the same folder structure in a temporary folder as well as to decrypt the files itself and save them into the corresponding folders of the temporary folder.
This part works well; the folder structure in a temporary file gets recreated as it should but the problem comes when I'm trying to create a ZIP file of a temporary folder using System.IO.Compression library.
When you open the ZIP file only the first folder of the structure is created as a folder and the rest of the folders are just being created as extensionless files.
Folder structure as the unzipping application sees it
Code snippet:
//Creating temporary folder where the decrypted files will be extracted
var tempFolderPath = _directory.CreateTemporaryFolder(rootFolderPath);
var serverTempFolderPath = HttpContext.Current.Server.MapPath(tempFolderPath);
//Creating the folder structure
RecursiveFolderCreate(folders, tempFolderPath);
//Decrypting the files and recreating them in the corresponding folders
foreach (var file in files)
{
PrepareFile(file, folderPath, tempFolderPath);
}
//Creating the zip file
string zipFileName = string.Concat(folder.Name, ".zip");
string zipFilePath = System.IO.Path.Combine(rootFolderPath, zipFileName);
string serverZipFilePath = HttpContext.Current.Server.MapPath(zipFilePath);
ZipFile.CreateFromDirectory(serverTempFolderPath, serverZipFilePath, CompressionLevel.Optimal, false);
byte[] zipFile = System.IO.File.ReadAllBytes(serverZipFilePath);
Sorry this isn't a comment, but I don't have enough reputation to comment yet.
Is it possible that an exception is being raised and not handled?
Per the documentation (https://msdn.microsoft.com/en-us/library/hh485721(v=vs.110).aspx/), what you are doing should work. But it has the extra caveat as listed below:
If a file in the directory cannot be added to the archive, the archive is left incomplete and invalid, and the method throws an IOException exception.

Categories