dotnetzip and comma separated files - c#

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!

Related

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

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!'

DotNetZip ExtractAll Zip64

I create a zip File using "UseZip64WhenSaving" option as true.
Once I try to extract it with "ExtractAll" method, an exception is thrown saying that the file "nameOfMyFile.z65536" does not exist.
It does not happen with files created as normal zip (not zip64).
Any suggestion to solve the issue?
The code creating the file:
using (ZipFile zCompressor = new ZipFile(strNameOftheZipFile))
{
zCompressor.UseZip64WhenSaving = Zip64Option.Always;
FileInfo[] fiArrayFiles = dInfoBCP.GetFiles("*.bcp", SearchOption.TopDirectoryOnly);
foreach (FileInfo fileTemp in fiArrayFiles)
{
zCompressor.AddFile(fileTemp.FullName);
}
zCompressor.MaxOutputSegmentSize = (700 * 984540);
zCompressor.Save();
}
The code to extract (that generates the error):
ZipFile zip = ZipFile.Read(FullNameOfmyZipFile);
zip.ExtractAll(strPathDest, ExtractExistingFileAction.OverwriteSilently);

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!

Ionic Zip - Pack whole directory keeping subdirectories and iterate through all files

I want to pack a directory in C# using Ionic Zip. Normally I would just use this piece of code:
using (ZipFile pack = new ZipFile())
{
pack.AddDirectory(defPackageCreationPath + "\\installfiles", "");
pack.Save(outputPath + "\\package.mpp");
}
This is working fine, however I need to iterate through each file being packed to check for characters in their filenames, as I have some files that gets corrupted when packed, if they contain specific characters.
What is important too is that the directory to add contains sub directories too, and those needs to be carried over to the zip file and created inside it.
How?
Not sure if this is what you are looking for but you can easily get a string array of all files including sub directories. using the Directory class
Like so
string[] Files = Directory.GetFiles(#"M:\Backup", "*.*", SearchOption.AllDirectories);
foreach (string file in Files)
{
DoTests(file);
}
This will include the path to the files.
You will need System.IO;
using System.IO;
You can also try something like this:
using (ZipFile pack = new ZipFile())
{
pack.AddProgress += (s, eventArgs) =>
{
// check if EventType is Adding_AfterAddEntry or NullReferenceException will be thrown
if (eventArgs.EventType == ZipProgressEventType.Adding_AfterAddEntry)
{
// Do the replacement here.
// eventArgs.CurrentEntry is the current file processed and
// eventArgs.CurrentEntry.FileName holds the file name
//
// Example: all files will begin with __
eventArgs.CurrentEntry.FileName = "___" + eventArgs.CurrentEntry.FileName;
}
};
pack.AddDirectory(defPackageCreationPath + "\\installfiles", "");
pack.Save(outputPath + "\\package.mpp");
}
}

Find all files in a folder

I am looking to create a program that finds all files of a certain type on my desktop and places them into specific folders, for example, I would have all files with .txt into the Text folder.
Any ideas what the best way would be to accomplish this? Thanks.
I have tried this:
string startPath = #"%userprofile%/Desktop";
string[] oDirectories = Directory.GetDirectories(startPath, "");
Console.WriteLine(oDirectories.Length.ToString());
foreach (string oCurrent in oDirectories)
Console.WriteLine(oCurrent);
Console.ReadLine();
It was not successful in finding all of the files.
A lot of these answers won't actually work, having tried them myself. Give this a go:
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
DirectoryInfo d = new DirectoryInfo(filepath);
foreach (var file in d.GetFiles("*.txt"))
{
Directory.Move(file.FullName, filepath + "\\TextFiles\\" + file.Name);
}
It will move all .txt files on the desktop to the folder TextFiles.
First off; best practice would be to get the users Desktop folder with
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Then you can find all the files with something like
string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
Note that with the above line you will find all files with a .txt extension in the Desktop folder of the logged in user AND all subfolders.
Then you could copy or move the files by enumerating the above collection like
// For copying...
foreach (string s in files)
{
File.Copy(s, "C:\newFolder\newFilename.txt");
}
// ... Or for moving
foreach (string s in files)
{
File.Move(s, "C:\newFolder\newFilename.txt");
}
Please note that you will have to include the filename in your Copy() (or Move()) operation. So you would have to find a way to determine the filename of at least the extension you are dealing with and not name all the files the same like what would happen in the above example.
With that in mind you could also check out the DirectoryInfo and FileInfo classes.
These work in similair ways, but you can get information about your path-/filenames, extensions, etc. more easily
Check out these for more info:
http://msdn.microsoft.com/en-us/library/system.io.directory.aspx
http://msdn.microsoft.com/en-us/library/ms143316.aspx
http://msdn.microsoft.com/en-us/library/system.io.file.aspx
You can try with Directory.GetFiles and fix your pattern
string[] files = Directory.GetFiles(#"c:\", "*.txt");
foreach (string file in files)
{
File.Copy(file, "....");
}
Or Move
foreach (string file in files)
{
File.Move(file, "....");
}
http://msdn.microsoft.com/en-us/library/wz42302f

Categories