I am using DotNetZip 1.9.6 in my application which uses a file structure similar to e.g. *.docx: Zip file containing XML files.
Now every module of the application can store such XML files to my custom file management and on "save" they are serialized to streams which are then saved to the Zip file via DotNetZip.
To update the entries I use ZipFile.UpdateEntry(path, stream).
This works fine and the first time I save my file via calling ZipFile.Save() everything works.
But If I do this a second time (first some UpdateEntrycalls then Save) on the same instance the Zip file is corrupted: The file structure and meta-data (e.g. uncompressed size of each file) is still there, but all files are 0 byte in compressed size.
If I create a new instance from the just saved file after saving everything works fine, but shouldn't it be possible to avoid that and "reuse" the same instance?
The following example (also see https://dotnetfiddle.net/mHxEIy) can be used to reproduce the problem:
using System.IO;
using System.Text;
public class Program
{
public static void Main()
{
var zipFile = new Ionic.Zip.ZipFile();
var content1 = new MemoryStream(Encoding.Default.GetBytes("Content 1"));
zipFile.UpdateEntry("test.txt", content1);
zipFile.Save("test.zip"); // here the Zip file is correct
//zipFile = new Ionic.Zip.ZipFile("test.zip"); // uncomment and it works too
var content2 = new MemoryStream(Encoding.Default.GetBytes("Content 2"));
zipFile.UpdateEntry("test.txt", content2);
zipFile.Save(); // after that it is corrupt
}
}
To run this you need to add the "DotNetZip 1.9.6" NuGet package.
After the first save, this is what you get:
and after the second save:
This looks like it's a bug in the library, around removing an entry. If you just remove an entry and then save again, it correctly removes the file.
However, if you remove an entry and then add another one with the same name - which is what UpdateEntry is documented to do if the entry already exists - the old entry appears to be used instead.
The reason you're ending up with an empty file the second time is that the original MemoryStream is being read again - but by now, it's positioned at the end of the data, so there's no data to read. If you reset the position to the start of the stream (content1.Position = 0;) it will rewrite the original data. If you modify the data within content1, you end up with invalid compressed data.
The only workaround I can immediately think of is to keep your own map from filename to MemoryStream, and replace the contents of each MemoryStream when you want to update it... or just load the file each time, as per your existing workaround.
It's definitely worth filing a bug around this though, as it should work as far as I can tell.
As already suspected this was a bug in DotNetZip up to version 1.9.6.
I think I was able to fix this with THIS change which was just released as version 1.9.7 on NuGet. At least for me the problem does not happen anymore.
Some background what happend as far as I found out:
When you call Save the library sets an internal flag which remembers that the ZIP file was just save and on the second Save call instead of "recompressing" all entries in the ZIP file it copies them from the just saved file.
This works fine for adding/removing entries, but breaks when one of the entries was changed as then it "mixes" the old and the new entry and produces the inconsisten ZIP file.
My fix basically disables that "copy from old file" logic if an entry was changed.
Related
I'm currently working with Ragnar which is a CLI Libtorrent wrapper.
I've hit a brick wall. Perhaps it's an implementation flaw of the wrapper I'm using, or I've simply misunderstood the Libtorrent API documentation, but I can't figure out how to properly save/load the current Session state data.
My current goal, as I can best state it, is to save all torrent_handles in the current session, so that when I next run the torrent client I am working on, I can load them automatically on startup and resume downloading/seeding.
I'm still unsure if I should do this by saving the Session state or not. As per the API documentation's wording:
The flags arguments passed in to save_state can be used to filter which parts of the session state to save. By default, all state is saved (except for the individual torrents).
But I can see no flag which pertains to individual torrents:
enum save_state_flags_t
{
save_settings = 0x001,
save_dht_settings = 0x002,
save_dht_state = 0x004,
save_proxy = 0x008,
save_i2p_proxy = 0x010,
save_encryption_settings = 0x020,
save_as_map = 0x040,
save_feeds = 0x080
};
Also, the wrapper is currently hard coded to not accept these flags:
cli::array<byte>^ Session::SaveState()
{
libtorrent::entry entry;
this->_session->save_state(entry);
return Utils::GetByteArrayFromLibtorrentEntry(entry);
}
This should be easy to fix, but am I missing something? Am I attempting to save via the wrong mechanism?
libtorrent does not provide a mechanism to save the torrent list. The expectation is that you (the client) keeps the .torrent files on disk (as they are immutable) and just re-add them the first thing you do when starting up again.
The one exception is when adding a magnet link, then you need to be able to turn a torrent_handle into an actual .torrent file. Here's a snippet to do that:
boost::intrusive_ptr<torrent_info const> ti = h.torrent_file();
create_torrent new_torrent(*ti);
std::vector<char> out;
bencode(std::back_inserter(out), new_torrent.generate());
save_file("mytorrent.torrent", out);
However, perhaps an even better option is to save the .torrent file (or info-dict) as part of the resume data. When calling save_resume_data(), if you pass in the save_info_dict flag, the resume data will contain everything you need to restart the torrent. i.e. an actual copy of the .torrent file will be saved inside the resume file.
The example that comes with libtorrent simply keeps .torrent files in a directory, and scans the directory on startup (and periodically), so the filesystem stores the torrent list. A more efficient way of doing it is to store the actual .torrent files along with the resume data in a database (say, sqlite).
Here's an example of saving the resume data bundled with the .torrent file inside a sqlite database.
save_resume.cpp, save_resume.hpp
The database makes for more efficient startup, when loading them all. Bundling the resume data together with the torrent also saves you one disk seek per torrent you load).
Basically, I'm building a website that allows user to upload file.
From the front end (JavaScript), the user will browse a file, I can get the site to send POST data (the parameter "UploadInput" and it's value, which the value is the file)
In the backend (C#), I want to make a copy of the file and save it in a specific path.
Below is the way I did it.
var files = Request.Files;
file[0].SaveAs("\temp\\" + file[0].FileName);
The problem I ran into is that I got the error message saying index out of range. I tried Response.Write(files.Count) and it gives me 0 instead of 1.
I'm wondering where I did wrong and how to fix it, or if there's a better way of doing it.
Thanks!
Edit:
I am using HttpFox to debug. From HttpFox, I can see that under POST data, parameter is "UploadInput" and the value is "test.txt"
Edit 2:
So I tried the way Marc provides, and I have a different problem.
I am able to create a new file, however, the content is not copied over. I tried opening the new created file in notepad and all it says is "UploadInput = test.txt"
If they simply posted the file as the body content, then there will be zero "files" involved here, so file[0] will fail. Instead, you need to look at the input-stream, and simply read from that stream. For example:
using(var file = File.Create(somePath)) {
Request.InputStream.CopyTo(file);
}
I am basically creating a xlsx file but I am getting an error while using that file like below.
System.IO.FileFormatException: Archive file cannot be size 0.
The way I tried.
string file = "c:\\DoneDone61.xlsx";
using(File.Create(file))
{
}
Also I cannot open excel file manually because it says the file is corrupted.
Thanks for answers in advance.
An Excel file which you consider to be "blank" is not just a file with no data in it (which is what you are creating). You can see this yourself by creating a document manually in Excel and then opening it in notepad. You'll notice that it actually has data inside of it. That data is used to store information regarding the three empty sheets named "Sheet1", "Sheet2" and "Sheet3". Also, there is some header information so that any program looking at the file knows that it is actually a compressed file (as per the Excel file format). So, as you can see, even a pretty empty excel file still contains SOME data.
If you want to create a blank excel document using C#, you have two good options:
Use a library that allows you to actually work with creating Excel documents that takes care of creating the file correctly. Check out something like the Microsoft OpenXML SDK or ExcelPackage.
Create an empty Excel document, store it somewhere, and when you want to "create" a new empty Excel document, just make a copy of this file.
This one ...
using(File.Create(filePath))
{
}
... creates an empty file. Read: really empty (=> 0 bytes) not an empty XLSX with an XLSX skeleton: ZIP container, file header, style definitions, ....
What exactly did you expect?
EDIT:
If you want to create an empty XLSX file (like "Right Click on Mouse > New > New Microsoft Excel"), you have to use such an template, ... and write it onto the disk.
To achieve that, you have to deploy this template file with your application, and then do a File.Copy(source, dest), or integrate it as a resource and write the resource content to the disk.
What you need to use is Interop.Excel namespace. Here's a guide from msdn
please use this...Its a bit hacky but couldn't get a better way to do this using InterOp
public static void CreateEmptyXLSXFile(string FilePath)
{
FileStream MyStream = new FileStream(FilePath, FileMode.CreateNew, FileAccess.ReadWrite);
MyStream.Write(ExcelDocumentsInterOps.GetEmptyXSLXFileBytes(), 0, ExcelDocumentsInterOps.GetEmptyXSLXFileBytes().Length);
MyStream.Flush();
MyStream.Close();
MyStream.Dispose();
MyStream = null;
}
/// <summary>
/// Returns the bytes for an empty xslx file
/// </summary>
/// <returns></returns>
public static byte[] GetEmptyXSLXFileBytes()
{
string TheSting = "UEsDBBQABgAIAAAAIQBYVsaPYAEAABgFAAATANoBW0NvbnRlbnRfVHlwZXNdLnhtbCCi1gEooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AYAAAAAAAAAAAAAAAAAJISAAB4bC93b3Jrc2hlZXRzL3NoZWV0My54bWxQSwECLQAUAAYACAAAACEAuEpLLRMBAAC3AQAAGAAAAAAAAAAAAAAAAADbEwAAeGwvd29ya3NoZWV0cy9zaGVldDIueG1sUEsBAi0AFAAGAAgAAAAhAAdfONMeAQAAxwEAABgAAAAAAAAAAAAAAAAAJBUAAHhsL3dvcmtzaGVldHMvc2hlZXQxLnhtbFBLAQItABQABgAIAAAAIQAQFQ8XQQEAAF8CAAARAAAAAAAAAAAAAAAAAHgWAABkb2NQcm9wcy9jb3JlLnhtbFBLAQItABQABgAIAAAAIQCcPLtYiwEAADQDAAAQAAAAAAAAAAAAAAAAAPAYAABkb2NQcm9wcy9hcHAueG1sUEsFBgAAAAALAAsAygIAALEbAAAAAA==";
return Convert.FromBase64String(TheSting);
}
When dealing with spreadsheet related tasks in .NET, you can use this open source library called SpreadsheetLight to write an excel file (especially, if you want to write content at some point).
If you prefer adding it as package via Nuget, you can say:
Install-Package SpreadsheetLight
After that, going by GenerateReport() exmaple:
// this one creates an empty workbook
using (SLDocument sl = new SLDocument())
{
// sl.SetCellValue("B3", "I love ASP.NET MVC");
sl.SaveAs("c:\\DoneDone61.xlsx");
}
Also see their tutorial for more interesting stuff.
I am having an xml file like:
<CurrentProject>
// Elements like
// last opened project file to reopen it when app starts
// and more global project independend settings
</CurrentProject>
Now I asked myself wether I should deliver this xml file with above empty elements with the installer for my app or should I create this file on the fly on application start if it does not exist else read the values from it.
Consider also that the user could delete this file and that should my application not prevent from working anymore.
What is better and why?
UPDATE:
What I did felt ok for me so I post my code here :) It just creates the xml + structure on the fly with some security checks...
public ProjectService(IProjectDataProvider provider)
{
_provider = provider;
string applicationPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
_projectPath = Path.Combine(applicationPath,#"TBM\Settings.XML");
if (!File.Exists(_projectPath))
{
string dirPath = Path.Combine(applicationPath, #"TBM");
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
using (var stream = File.Create(_projectPath))
{
XElement projectElement = new XElement("Project");
projectElement.Add(new XElement("DatabasePath"));
projectElement.Save(stream, SaveOptions.DisableFormatting);
}
}
}
In a similar scenario, I recently went for creating the initial file on the fly. The main reason I chose this was the fact that I wasn't depending on this file being there and being valid. As this was a file that's often read from/written to, there's a chance that it could get corrupted (e.g. if the power is lost while the file is being written).
In my code I attempted to open this file for reading and then read the data. If anywhere during these steps I encountered an error, I simply recreated the file with default values and displayed a corresponding message to the user.
I start my application from withint Visual Studio 2010.
I add then some files into my application and each file type`s icon like icon from doc,docx,xls,pdf etc are added as String/Bitmap key/value pair to my IconImages.Resx file via
private void DumpTempResourceToRealResourceFile(IDictionary<String, Bitmap> tempResource)
{
using (ResXResourceWriter writer = new ResXResourceWriter("IconImages.Resx"))
{
foreach (KeyValuePair<String,Bitmap> item in tempResource)
{
writer.AddResource(item.Key, item.Value);
}
writer.Generate();
}
}
When the icons are added to the resource I close the application.
Then I start my application again with VS 2010 and add some files within my document application. The file types are written again to my IconImages.Resx.
Then I close my application and check the IconImages.Resx file under the \bin\ folder and the previous saved images are gone and I have new/different ones now.
Why can I not say OPEN a .resx file and append stuff to it? Everytime I create a ResourceWriter object with the same name "IconImages.Resx" I overwrite the previous added stuff and thats stupid.
How can my IconImages.Resx file stay alive over an application session without being overwritten by other stuff I add?
I haven't used ResXResourceWriter, but usually *Writer classes simply write a data file from scratch.
If you want to "append" new data you would typically have to use a *Reader class to deserialise the existing data into memory, then merge/add in any new data you wish to, and use a *Writer object to then write the resulting data back out. Take a look at ResXResourceReader to see if it supports what you need to do this.
I am having now a lookup table "FiletypeImage" with the filetype ".docx" and the raw binary data aka blob. This table gets retrieved in my documentService and cached in a static variable. with a Get and Add method which are called by my DocumentListViewModel. Its very fast thx to sqlite :)