Get content of zip file - Character encoding

Hi Experts

I need to read the content of a zip file and output the files within the zip file to a list. I have managed to do that with an Invoke Code activity adding the following code snip:

List<string> fileNames = new List<string>();

try {

	using (ZipArchive zipArchive = ZipFile.OpenRead(in_ZipFile)) {
	
		foreach (ZipArchiveEntry entry in zipArchive.Entries) {
		
			fileNames.Add(entry.FullName);

		}
	}
	
	out_FilesList = fileNames;
	
}

catch (Exception ex) {
	Console.WriteLine(ex.Message);
}


foreach (string fileName in fileNames) {
	
	Console.WriteLine(fileName);
	
}

To code snip above works but there seem to be an issue with non A-Z characters e.g., æ, ø and å being replaced by �. Clearly this has to do with the encoding.

When using a Python script I can see that using code page 437 the characters are displayed correct in the Python terminal.

In the Invoke Code I have then tried adding the following line:

string decodedFilename = System.Text.Encoding.GetEncoding(437).GetString(System.Text.Encoding.GetEncoding(437).GetBytes(entry.FullName));

But now the characters are being replaced by a regular question mark.

Any ideas on how to get the characters to show correct in UiPath as well?

Have you tested to provide the encoding in the ZipArchive constructor instead?

...

	using (FileStream zipFileStream = File.Open(in_ZipFile, FileMode.Open)) {
		using (ZipArchive zipArchive = new ZipArchive(zipFileStream, ZipArchiveMode.Read, false, System.Text.Encoding.GetEncoding("437"))) {
				foreach (ZipArchiveEntry entry in zipArchive.Entries) {			
					fileNames.Add(entry.FullName);
			}
		}
	}

...
1 Like

Thanks @ptrobot that does exactly what I needed :+1:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.