WinRT ZipArchiv include Folder informations - c#

With Ziparchiv.CreateEntry I add files to a zipStream
But is there a way to create also folders?
I want to zip a folder tree to a zip. Currently I can't see a way to have the sametree in the zip file.
The only soultion I currently have , to not loose the information, is to create somekind of reference file which contrains the relative pathes.
Is there a way to have folders in WinRT ZipArchivs?

Just found the solution when writing the sample code.
Because i didn't find this here i consider adding the question also hoping that this will help someone.
If this one is a duplicate feel free to delete it or link it to the pre existing question :-)
When calling CreateEntry the path can be prefixed to the filename.
The following code sets foo as the folder in the zipfile in the archive
zipArchive.CreateEntry(#"foo\" + fileToCompress.Name,CompressionLevel.Optimal);

Related

How and in which folder should I store the file? The file will need to delete and add data

How and in which folder should I store the file(xml)? The file will need to delete and add data using "linq to xml"
I use Visual studio. Cross-Platform project.
Assuming that this is not a user document (like a Word doc), but rather a settings file, then the most appropriate place to store it would be in the user's application data folder, which is one of the folders under C:\Users\username\AppData
You would find this as follows...
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
However, a bit more information about what you're actually trying to achieve might help you get a better answer.
Hope that helps

Add Multiple Folders / Directories to a ZipFile

I am writing a backup program that requires predefined multiple folder(s) & single file(s) to be added to a single zip archive. I have had no issues adding a single folder using -ZipFile.CreateFromDirectory(string, string, c..level, bool(false))
However i am having a hard time adding multiple folders as there does not seem to be a way to update an archive or target two folders using the CreateFromDirectory method.
- Would be nice if there was an UpdateFromDirectory mehod!
I have been trying to stay away from third party libraries for no reason really, however as far as i have found none deal with multiple unrecursive folders.
I have tried just about everything other than writing my own code to recurse & add individually which i don't really want to do.
The program has several inputs that defines the folders / files to be zipped and depending on whether they are not null should add them to a single zip file regardless of whether they are a folder or file.
I guess my question is whether this is possible at all using the boxed libraries without custom recursing or even with a third party library without heavy mods... Not sure if i have made my question clear, sure you will all let me know if i have not.
From what I can tell using the ZipFile class you can only create and read. if you want to update you would need to create the whole zip again. [Source: ZipFile methods]
to target more than one folder you could arrange all the files and folders into one folder then zip the entire source without including the source folder. In most cases moving this files/folders isn't possible so I'd recommend looking into Symlinks within windows. I'd redirect to you [Issue with creating symbolic link to directory
You can create a "myFolder" folder and put in it all the folders you want found in the zipped folder. Then do ZipFile.CreateFromDirectory("myFolder", "name of zip file to create", CompressionLevel.Fastest, false, Encoding.UTF8). Overriding this IncludeBaseDirectory method to false allows this to be done.

How to implement a text file in content (XNA)

I am building a game which loads its map from a text file.
While creating the parts that handle maps, I simply kept the text file in the content folder and fetched it by its Windows filepath. This won't work for proper deployment (or even running the game from different drives) because it requires that the filepath be exactly the same.
I looked around for a way to include the text file the same way I would a Texture2D, but I cannot find any class that allows me to use it. Some answers to other questions suggested that I just use the text file from my content folder? How would I do that? My program's name is IslandQuest (placeholder; it doesn't even involve an island) so would I place the text file in the IslandQuestContent folder generated by XNA Studio? How would I access it from there such that its filepath doesn't depend on the drive configuration of a computer?
Thanks for any help.
This may not be the best way to do this but just looked back at what I did in my first year at university with XNA,
I added my txt file to the contents folder. Then in the properties for the file (select it in the solution explorer and view properties window) there should be "Copy to Output Directory", make sure this is copy if newer.
Then its just a case of
string.Format("Content/{0}.txt", filename)
I do think this can be improved perhaps by the following but it is untested
Path.Combine(Content,filename +".txt");
In my case I was reading XML file files from a data folder in my main project.
So under my project in Solution explore I had this set up:
WindowsPhoneGame1
...
data/
content.xml
Game1.cs
Program.cs
etc...
Where properties for content.xml were Build Action: Content and Copy to Output Directory: Copy always
In the class that read the file I used TitleContainer.OpenStream Method which according to the docs:
Returns a stream to an existing file in the default title storage
location. .... The stream returned is read-only. The file to be read must already exist, or this method will fail.
My example code
//open stream
Stream stream = TitleContainer.OpenStream("data/content.xml");
//do something with it...
Create a "Content" folder in your main project.
Put the files that cannot be put in the Content project in there.
Be sure to set all your content Build actions to Content and Copy Always.
The Content folder from your main project and the content in the content project will end up in the same folder when built.
The file path would still be "Content/file.ext" or whatever.

How to merge 2 zip files together into 1 zip

I am trying to make a custom launcher for Minecraft in C# but I have come across a bump.
I want to add something into it, Minecraft Forge, but the only way I could think of is to change the extension of minecraft.jar to minecraft.zip, extract the contents of the Minecraft Forge.zip and the minecraft.zip into the same folder and then zip that entire folder up into minecraft.jar.
However minecraft.jar has a file named aux.class so whenever my extract script (Made in java) tries to extract it, it simply says:
Unable to find file G:\Programming\C#\Console\Forge Installer\Forge Installer\bin\Debug\Merge\aux.class.
The only other way I can think of is to merge minecraft_forge.zip into minecraft.zip, I have spent around 2 hours looking on Google (watch as someone sees it within a couple of minutes) but it always shows me results for "How to zip multiple files", "How to make a zip file in C#" etc.
So I have come here looking for my answer, sorry if this is a lot to read but I always see comments on here saying "You didn't give enough information for us to help you with".
EDIT: The question in case it wasn't clear is: How am I able to put the contents of minecraft_forge.zip into minecraft.zip?
In your case, if you cannot unzip the files due to OS limitations, you need to "skip" unzipping temporary files to zip them. Instead, only handle input & output streams, as suggested in the answers found here: How can I add entries to an existing zip file in Java?
As you pointed out, "aux" is a protected keyword within windows and it does not matter what the file suffix may be; windows won't let you use it. Here are a couple of threads that discusses this in general.
Ref 1: Windows reserved words.
Ref 2: Windows reserved words.
If you are typing in commands to perform the copy or unzip, there is a chance you can get this to work by using a path prefix of the following \\.\ or \\?\. When I tested this, it worked with either a single or double back-slash following the period or question mark. Such that the following work:
\\.\c:\paths\etc
\\.\\c:\paths\etc
\\?\c:\path\etc
\\?\\c:\path\etc
I used the following command to test this. When trying to rename through windows explorer it gave a "The specified device name is invalid." error message. From the command line it worked just fine. I should point out, that once you create these files, you will have to manually delete them using the same technique. Windows Explorer reports that these text files which have a size of 0 bytes "is too large for the destination file system", ie... the recycle bin.
rename "\.\c:\temp\New Text Document.txt" aux.txt
del "\.\c:\temp\aux.txt"
As far as copying directly from zip or jar files, I tried this myself and it appeared to work. I used 7-zip and opened the jars directly using the "open archive..." windows explorer context menu. I then dragged-and-dropped the contents from forge.jar to the minecraft jar file. Since it is the minecraft jar file with the offending file name the chance of needing to create a temporary file on the filesystem is reduced. I did see someone mention that 7-zip may extract to a temporary file when copying between jars and zips.
7-zip reference on copying between archives
I should point out that my copy of minecraft jar (minecraft_server.1.8.7.jar) did not contain a file named aux.class. I also did not try to use the jar after the copy/merge. Nor did I spend too much time trying to figure out how well it merged the two contents since it appears like there may be a conflict with com\google\common\base\ since there are similar class name but with different $ variable suffixes on them.
I hope these two possible suggestions could give you some room to work with to find a solution for your needs... if you're still looking.

How do I copy folder(and subfolders) from my solution content into IsolatedStorage?

I have a folder with content in my solution, it contains lots of subdirectories. I need to copy this folder into IsolatedStorage. I read msdn forum message where it was said that there are no means to get folder content from code. How do I solve this problem?
The forum ist right. There is no way to enumerate "Content" resources.
You could set build action "Resource" or "Embedded Resource" as some answers suggest - then there are ways to enumerate the resources using ResourceManager or similar means. But I wouldn't recommend it as this will embed all resources into your assembly bloating it and making your app slow to load.
Here is a similar question (about enumerating image files). No solution though. The answer of Matt contains the only workaround: Prepare a list of filenames at design time and build this list into the app. Then instead of enumerating the files at runtime you read the filenames one by one from this list.
If you need this just for developing and testing then like the others I also recommend looking at the ISETool. You setup your app once with a reference storage and use the tool to save the state of the isolated storage. When you need to restore the state from isolated storage you can use the tool to copy the saved one back to the phone or emulator. An example for doing this can be found in this blog post.
Do you have a lot of files as well in the subdirectories? I see three solutions:
Set the file build action as Resource so they are embedded in the dll. You can retrieve the folder anme in the filename of the resources (MyAssembly.MyFolder.Filename.extension).But it will slow down the loading of your assembly and so the startup time of your application.
Set the files build action to Content so they are included in the XAP file but I'm not sure you can iterate the content without knowing the path
You can put the content in a zipped file on a remote server, get it on the first startup and use http://slsharpziplib.codeplex.com/ to dezip the content in Isolated Storage.
Mighter,
If I understand you right, you need the contents of your folder, along with the sub-directories in Isolated Storage, right? Putting them in your solution only gets them into the XAP.
You could use the Isolated Storage Explorer that comes bundled with the Windows Phone SDK 7.1 to manipulate file storage in Isolated Storage. This would be the easiest way to get your folder's content into Iso.
You could start learning about Isolated Storage Explorer [ISETool.exe] from here.
Hope this helps!

Categories