File name with special characters( å, ä, ö ) are unable to read by ionic.zip - c#

I'm trying to read filename with Swedish characters. When the files are being read I get the exception file not found. The same work for file names without special characters.
I tried with different encoding but still the file names was not being read. Is there a way to handle this
Im running the code on Windows 10.
using (var zip = new ZipFile())
{
zip.AlternateEncoding = Encoding.UTF8;
zip.AlternateEncodingUsage = Ionic.Zip.ZipOption.Always;
zip.UseUnicodeAsNecessary = true;
string filepath = #"C:\Users\username\rel\Reådmeö.txt";
zip.AddItem(filepath, Path.GetFileName(filepath));
zip.Save(zipFileSavePath);
}
following is the error
System.IO.FileNotFoundException: 'That file or directory (C:\Users\username\rel\Reådmeö.txt) does not exist!'

Related

tarArchive ExtractContents() make Exception creating directory '*' because a file or directory with the same name already exists

I wrote a program with asp.net core I would extract tar archive files and I have used this method.
tarArchive.ExtractContents(destPathFolder)
but I got error Exception creating directory '*' because a file or directory with the same name already exists. I don't know how to resolve this issue I have done a lot of searches but I couldn't solve my issue.
for example, the blow address is a place I would extract my files.
C:\data\634192CC-FDCB-4711-BD41-B0F7A7E77148\BAF59489-C7F9-4747-A0A1-B3C7A94FFFD4-2019-12-08
14-47-19\task-2019-12-08 21-13-20
this method extract tar archives files that have text files. It means if we have image files we will get that error.
static void ExtractTar(Stream inStream, String destFolder)
{
string sourcePath = Path.Combine(destFolder , "/data" );
Stream tarStream = new TarInputStream(inStream);
TarArchive tarArchive = TarArchive.CreateInputTarArchive(tarStream);
tarArchive.ExtractContents(sourcePath );
tarArchive.Close();
tarStream.Close();
inStream.Close();
}
I have changed to this code but I have got Access path to "" is denied.
static void ExtractTar(string addressFile, string destFolder)
{
Stream inStream = System.IO.File.OpenRead(addressFile);
TarArchive tarArchive = TarArchive.CreateInputTarArchive(inStream);
tarArchive.ExtractContents(destFolder);
tarArchive.Close();
inStream.Close();
}

Unable to read textfile with special characters from a directory

I'm trying to read a textfile name with special characters(Ex: Student_*_Details.txt) from a directory. When I try to read them it says it has illegal characters in the path as the textfile name has these special characters and unable to read the file.
Code:
using (StreamReader streamReader = new StreamReader(#"\\NKR1009FHN\Student_*_Details.txt"))
I' have used the below set of codes to remove the illegal characters from the filename. But in the streamReader if fails as the special characters are getting replaced and unable to find the file since the actual filename got changed due to the below method used.
private static string GetValidFileName(string fileName)
{
String ret = Regex.Replace(fileName.Trim(), "[^A-Za-z0-9_. ]+", "")
return ret.Replace(" ", String.Empty);
}
Is there any workaround to get this fixed?
Really appreciate suggestions.

Reading a file from resources

I have embed sample.txt(it contains just one line "aaaa" ) file into project's resources like in this answer.
When I'm trying to read it like this:
string s = File.ReadAllText(global::ConsoleApplication.Properties.Resources.sample);
I'm getting System.IO.FileNotFoundException' exception.
Additional information: Could not find file 'd:\Work\Projects\MyTests\ConsoleApplication\ConsoleApplication\bin\Debug\aaaa'.
So seemingly it's trying to take file name from my resource file instead of reading this file. Why is this happening? And how can I make it read sample.txt
Trying solution of #Ryios and getting Argument null exception
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApplication.Resources.sample.txt"))
{
TextReader tr = new StreamReader(stream);
string fileContents = tr.ReadToEnd();
}
The file is located in d:\Work\Projects\MyTests\ConsoleApplication\ConsoleApplication\Resources\sample.txt
p.s. Solved. I had to set Build Action - embed resource in sample.txt properties
You can't read Resource Files with File.ReadAllText.
Instead you need to open a Resource Stream with Assembly.GetManifestResourceStream.
You don't pass it a path either, you pass it a namespace. The namespace of the file will be the Assemblies Default Namespace + The folder heieracy in the project the file is in + the name of the file.
Imagine this structure
Project (xyz.project)
Folder1
Folder2
SomeFile.Txt
So the namespace for the file will be:
xyz.project.Folder1.Folder2.SomeFile.Txt
Then you would read it like so
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("xyz.project.Folder1.Folder2.SomeFile.Txt"))
{
TextReader tr = new StreamReader(stream);
string fileContents = tr.ReadToEnd();
}
Hello the proposed solution doesn't work
This return null:
Assembly.GetExecutingAssembly().GetManifestResourceStream("xyz.project.Folder1.Folder2.SomeFile.Txt")
Another way is to use a MemoryStream from the Ressource Data:
byte[] aa = Properties.Resources.YOURRESSOURCENAME;
MemoryStream MS =new MemoryStream(aa);
StreamReader sr = new StreamReader(MS);
Not ideal but it works

dotnetzip and comma separated files

I'm developing a console application with dot net that reads 7zip compressed csv files, and load csv file into a DB. It works well with tab separator, with special characters, but if the file has the "comma" separator, there is an error message "Bad signature".
The code is below:
string DirPath = "myPath/myFolder";
DirectoryInfo dir = new DirectoryInfo(DirPath);
FileInfo[] FileList = dir.GetFiles("*.7z", SearchOption.AllDirectories);
foreach (var fileZip in FileList)
{
try
{
using (ZipFile zip = ZipFile.Read(fileZip.FullName))
{
var a = zip.Entries.Where(p => p.FileName.EndsWith(".txt") || p.FileName.EndsWith(".csv")).ToList();
In the portion of code above, the error "bad signature" is thrown on the using statement, and only if the compressed file has a comma separator.
Do you have any suggestion? Have you never ever face anything like that?
Thanks in advance for your support!
Cheers!

Set unicode encoding on all files in folder with c#

I have this folder with a bunch of csv files, that I want to import into SQL Server. That works fine with BULK INSERT.
However I have a problem with the encoding, getting weird charters in the db - if I open the csv files in notepad, and save them again as unicode, it works perfect.
Is their a way in C# to programmaticly, convert all files in a folder to unicode?
Thanks
For all files:
string text = File.ReadAllText("data.txt", Encoding.ASCII);
File.WriteAllText("data.txt", text, Encoding.Unicode);
To expand on #Sandre's answer, you can bulk this operation like this:
var di = new DirectoryInfo(#"path\to\csvFolder");
var csvFiles = di.EnumerateFiles("*.csv", SearchOption.TopDirectoryOnly);
var outputFolderDi = new DirectoryInfo(Path.Combine(di.FullName, "outputFolder"));
outputFolderDi.Create();
foreach(var filePath in csvFiles.Select(fi => fi.FullName))
{
var text = File.ReadAllText(filePath);
var fileName = Path.GetFileName(filePath);
var newFilePath = Path.Combine(outputFolderDi.FullName, fileName);
File.WriteAllText(newFilePath, text, Encoding.Unicode);
}
I make no guarantee that this code is correct. Please check carefully before deploying on your file system!

Categories