Error trying to send zip file to navigator - c#

I'm trying to send a zip file to navigator in ASP NET Visual Basic, I do the following:
Response.Clear()
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.ContentType = "application/zip"
Response.WriteFile(fullPath)
Response.End()
I get this strange characters
How can I send the zip to navigator?

A zip file is NOT a text file. Don't treat it like a text file. Don't try viewing it like a text file.
Rather, a ZIP file is a binary file format. It is a container (also often called an archive) that contains compressed files. What you see there in your screenshot is just the binary data of the ZIP file. Binary data which whatever viewer you used tried to interpret as character codes -- which of course will not yield any sensible result, because a ZIP file is not a text file. It's not.
If you want to get your hands on the files compressed within the ZIP file, you need to uncompress ("unpack") the ZIP file.
By the way, i can see that the compressed file within the ZIP file is presumably a PDF file. Which is also not a text file, but requires a PDF reader/viewer to display/render it (after it has been unpacked from the ZIP file, obviously...).

Related

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.

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

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;

Outputting .RTF file to response Corrupt

I have an AJAX file uploader on my page, and use this to store various different file types into a database, However when I store a .rtf file and then try to output this using response, I get a file corrupt and cannot be opened error message. The file uploader automatically detects the mime type of the file on upload and stores the file as "application/msword" as this didn't work I tried giving it several other mime types however none of them worked.
Does anyone know of any reasons why this file may be corrupt?
PS. I have also tried uploading several different .rtf files with text, no text and images, etc. however none of them worked. output code below:
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentType = displayFile.MimeType;
context.Response.AddHeader("content-disposition", "attachment; filename=" + displayFile.FileName);
context.Response.BinaryWrite(displayFile.Data);
context.Response.End();

Downloading file from database where file is stored in binary format

I have got stuck in a problem while downloading documents from database.
Currently I'm working in ASP.net project and this is my first career project.
We have some documents which we store in database. The documents(.pdf,.doc,.png,.docx,xls,xlsx) are stored in binary format with their type specified.
I can download one document using Response.write. But now i have to concatenate some documents and then allow user to download on button click.
I have googled a lot. Developers have said that this is impossible. But still i feel that this can be done.
However if this is impossible i thought that i will save these individual documents first at some server location and then zip them and then allow user to download. But how would i be able to save the individual document in their original format at server location.
Please help me out. I'm in big problem.
It is not impossible to read more than one document from a database and zip them up before presenting the zip file to the response stream. You can do this all in process, there is no need to save the documents to the server.
This code uses Ionic.Zip to zip up several files and write them to a MemoryStream:
foreach (var file in files)
{
zipFile.AddEntry(file.FileName, file.ContentBytes); // these are the file bytes
}
var zipMs = new MemoryStream();
zipFile.Save(zipMs);
zipMs.Seek(0, SeekOrigin.Begin);
zipMs.Flush();
The file.FileName includes the extensions (.docx) of the documents and when downloading through the browser, everything is displayed and saved correctly.

Amazon S3 - MS office files corrupted when downloading

When I upload a docx, xlsx or pptx to amazon S3 using aws .net sdk the file is getting uploaded fine and I am able to view the file directly from S3 without any issues. But when I download the file in ASP.net using C# I am getting a warning message (see below) when opening the file:
"Excel found unreadable content in test.xlsx. Do you want to recover the contents of this workbook?" If I click yes, I am able to see all the contents in the document.
similarly I am getting a warning message for .docx file as well.
This is the C# code I am using:
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.OutputStream.Write(Content, 0, Content.Length);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
Update: When I download the file in windows forms app it is working fine and the problem is with web app only.
What am I doing wrong?
Please help...
More information is needed to figure this out. The code snippet you provided looks OK. The problem looks to be elsewhere in your code... For example, what are you loading inside "Content"?
Also, it would help if you tried uploading and downloading a simple text file, to see what kind of data gets appended.
If Content is a byte[] created from a MemoryStream, make sure you're calling .ToArray() and not .GetBuffer() to get it. If you use .GetBuffer(), you'll potentially get extra bytes on the end, which the office file format probably doesn't like. Your other file types you've tried it with might be more tolerant of extra data they weren't expecting on the end.

Categories