I'm running into a strange problem using ZipFile and ZipArchive with .Net 4.5.
ZipFile.CreateFromDirectory takes all content of a directory, including folders that are empty.
If I try to create the same zip file using Windows explorer by right clicking > Send to > Compressed folder, I get a warning message saying the empty folder was omitted.
I'm loading the resulting zip file into an application that runs on Apache Tomcat. This application throws errors for every single file contained in the zip that I produced with ZipFile.CreateFromDirectory. The zip that I created manually through Windows explorer is read just fine.
I suspect the problem lies in the empty zipped folders, but haven't yet been able to definitively conclude this. If the empty folders are the cause, I'd need a way to use ZipFile.CreateFromDirectory excluding empty folders.
Taken from my comment above:
I have no .NET 4.5, but from the remarks section: "The directory structure from the file system is preserved in the archive. If the directory is empty, an empty archive is created." So this is by design.
So you either have to
fix it in the comsuming app on tomcat or you have to
create a temporary folder which just contains the non-empty folders, if possible
I haven't found a way to exclude empty folders in CreateFromDirectory in the first place.
Alternatively, I can remove empty directories from the created zip file. Though this still causes errors in the Tomcat application.
// compress and copy new zip
ZipFile.CreateFromDirectory(dirtocopy.FullName, NewZipFilePath);
using (ZipArchive za = ZipFile.Open(NewZipFilePath, ZipArchiveMode.Update))
{
// only empty folders end with \
List<ZipArchiveEntry> emptyFolders = (from ZipArchiveEntry zae in za.Entries
where zae.FullName.EndsWith("\\")
select zae).ToList<ZipArchiveEntry>();
emptyFolders.ForEach((ZipArchiveEntry folder) => folder.Delete());
}
Related
I have a very simple .NET console application in Visual Studio. I am trying to write some words into a text file.
using (StreamWriter file = File.AppendText("log1.txt"))
{
file.WriteLine("Hello from the text file");
}
If the file does not exist, the application creates it in the autogenerated folder bin/Debug.
Is there a way to create this file in the project's directory, where I have .csproj file?
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes, but this can only be done while you are working on your project. Once you are done developing it and try to publish it you won't have access to the location where you have .csproj file, because after publishing you can install it on any PC and it wont have the project you are working on.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug?
No, I assume by real-world applications in your context you mean a published project '.exe' that you can run on any PC. Windows provides you three Data folders that you should use when writing your program so that it works smoothly after publishing:
User Data
Roaming User Data
All User Data
You can acess the above folders in .NET application using the Environment.SpecialFolder:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
As per your given code, try this :
var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"log1.txt");
using (StreamWriter file = File.AppendText(fileName))
{
file.WriteLine("Hello from the text file");
}
This way you will be able to publish your program and it will still work smoothly without hard-coding the path as you were doing previously.
That's why .NET creates them there firstly?
If you don't specify a complete path, and just the file name .NET looks into the working directory of the executable, which in this case is bin/Debug
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes. As explained here (second answer) you can use the post-build event to write down the value of $(ProjectDir) in a text file (using command echo $(ProjectDir) > ..\..\projectdir.txt). This macro contains the directory of your .csproj. This command will create the file projectdir.txt with your project directory after a build process so you read this file contents in your code and use what is inside it to pass to File.AppendText as the base directory to create your file log1.txt.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
That depends on what you want to do. In your case the code creates the file at bin/Debug because that is where your executable are being executed. When you omit the full path to File.AppendText and just pass "log1.txt" as argument, it will create the file in the same folder as the executable are at. If you want a different folder you should specify the folder here (e.g. File.AppendText("C:/log1.txt") will create the file at C:/.
You can create the text file in the root of your project and use copy always to have them in the same place as your executable. If this is just a readonly text file then it's OK because windows doesn't allow you to modify the files reside in Programs folder in OS drive.
If you want your code to modify these text file then you need to put them in appdata folder. In real world example I did this on many project. All the database work my winforms, WPF application need goes in AppData folder.
I'm trying to load and save an xml file called Modules.xml in my code. I have currently got the file path hardcoded as shown below. I am trying to get the file path within my code without it being hardcoded.
I have tried using Path.GetDirectoryName and new FileInfo("Modules.xml").Directory.FullName. However, both of these target the file in my debug folder, when the file I need is in the main solution folder.
Is there a way to target the file in my main solution folder instead of my debug folder? (both files are called Modules.xml)
doc.Save("C:\\Users\\Matthew\\Desktop\\Year4\\Object Oriented\\Project1\\Project1\\Modules.xml");
Both file locations are shown below:
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\Modules.xml
^^^this is the file path I need for my code^^^
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\bin\Debug\Modules.xml
The best approach here would be to use a configuration file, e.g. app.config, for storing such a string. Then you can change file path without recompiling the code, and your file can be stored in any location accessible by application.
If you really want to access your file the way you explained, AppDomain.CurrentDomain.BaseDirectory will provide you with the bin/Debug location in runtime. Then you can find a relative path from there like:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, #"..\..\", fileName);
where fileName is "Modules.xml" for example.
I have tried using Path.GetDirectoryName and new
FileInfo("Modules.xml").Directory.FullName. However, both of these
target the file in my debug folder, when the file I need is in the
main solution folder.
That's because bin\Debug is your working directory when you start and run the project. To change that, you can set the working directory environment variable to point to your solution directory (instead of bin\debug|release) which I wouldn't recommend that. Because when you finally endup with development, and release the application, there wouldn't be any solution directory that holds your XML file. What I can suggest is to copy your XML file to the output folder. Either you are in development (debug) or production (release) mode, the XML always going to be copied to final directory. And you can access the working directory with something like AppDomain.CurrentDomain.BaseDirectory. To enabling copy XML to output directory, right-click on it, choose Properties, set Build Action to None, and set Copy to Output Directory to Copy Always or Copy if newer. You're good to go now.
I am having strange problem while updating my application through ftp. I am uploading files to a folder in ftp by Ipswitch WS_FTP.
I am updating files in remote folder to check my code. But it is downloading old files which were rewritten in remote folder.
Here is details. I have a folder update. Inside of this folder 2 files. version.txt and app.zip
In old files I used capital letters such as VERSION.txt, In new file small capitals.
But I can not reach new files. Why? I can reach only old files which is VERSION.txt
With app.zip the same problem, I am updating its files inside zip file. But getting old files from zip file.
PS. I am changing files inside app.zip and uploading it to remote folder but I can not reach new files inside app.zip. I can reach only old files. The problem is refreshing of files inside app.zip. Or I have problem with cache or something else?
If the server is on linux, you should note linux is file system is camel case (window is not), so in linux file.txt != FILE.txt and in windows are the same file. Hope this tips, helps.
I have an application that upon execution It copies two folders with subfolders that are in the same location to another windows location %AppData%
Now I have the following files :
MyApp.exe , Folder1, Folder2
In each folder there are subfolders. How to embed these two folders as resources inside the application so after compiling the program, I get only one executable file. And when I click on it, it extract the two folders to the same location then do the rest of job.
I know how to add a file as embedded resource then retrieve it using reflection,
but how about a folder Is that even possible??
I had to solve this problem recently. I embedded a ZIP file, and then decompressed it at runtime.
.NET 4.5 includes ZIP functionality. If not, use SharpZipLib or DotNetZip.
I have a folder with a list of files that I've included in my silverlight solution as "Content". I know how to load individual files using Application.GetResourceStream(), but I wish to load an entire directory with multiple files in it.
Attempted to use DirectoryInfo but it throws a Securityexception, and using Application.GetResourceStream() with a directory path returns null.
I solved by using a standard file naming convention, with a specified index in the filename, and then I just build each filename with using a loop and the index of the loop and load each stream into an array.