ZipArchive, 3mf and compression

Stefan

Member
Joined
Jun 1, 2021
Messages
7
Programming Experience
1-3
Hi there,
I have a very weird problem I just can't wrap my head around and not even sure where to ask this.
Basically I'm writing a converter for CAD models to the .3mf file format.
.3mf files are renamed .zip files that have a fixed folder structure and use .xml for their internal data.

I use the official 3mf examples for testing my code and here is the problem:
If I use the windows UI to select the files and rightclick -> 'send to zip archive' everything works correctly.
If I use c# ZipFile or sharpziplib to zip the files - the .3mf throws an error.
If I use tar.exe to zip the files the Windows 3D-viewer throws an error but other CAD programs can open the file.

Hp has a filechecker to look for errors in 3mf files and it throws:
the file is in the archive and is in the right place but for some reason wont be found by the interpreter.

Is there someone that can point me in the right direction?maybe zip files have different was to store path information that is not visible to me as the user?
 
guys, guys GUYS!
this is it!
I used ZipArchive to insert every file on its own with "/" as path-separator and it works.

C#:
ZipArchive zip = System.IO.Compression.ZipFile.Open(zipPath, ZipArchiveMode.Create);
zip.CreateEntryFromFile(tempPath+@"/[Content_Types].xml", @"[Content_Types].xml");
zip.CreateEntryFromFile(tempPath+@"/_rels/.rels", @"_rels/.rels");
zip.CreateEntryFromFile(tempPath+@"/3D/3dmodel.model", @"3D/3dmodel.model");
zip.Dispose();

Jesus Christ this was a stupid waste of time. Thanks everyone. I'll just leave some keywords here in case other people are looking for a solution.

zip 3mf files with C#
3mf compression not working
3mf file model could not be loaded 3D-Builder
Writing 3mf files from scratch zip c#
 
Mark p1/#11 as the solution. Well done man, I don't know how you stumbled on that. (y)

When was the Compression added into S/IO @JohnH? - I never recall using those, or maybe I did, but certainly don't remember.... my brain turns into a mushroom every summer with the heatwaves we seem to get every year now. I don't cope well in hot weather. ? It makes me forgetful and frustrated!

The inbuilt .net compression seems to work better for comp/decompressing than the available compression libs which is what originally gave me the same trouble as above.
 
, I don't know how you stumbled on that. (y)

When was the Compression added into S/IO @JohnH?
By looking at the entry names in post 9 (after I posted it).
DeflateStream/GZipStream has been there since .Net 2,, but ZipArchive/ZipFile was added as late as .Net 4.5 (2012). (look at the Applies To section for classes at MS Docs)
There's also the System.IO.Packaging namespace that deals with the exact kind of packaging that 3mf uses - these classes are more difficult to work with and has been around since .Net 3.0. I actually used them for "zipping" in a project long time ago.
 
I can finally dig up my 5 year old zips and unzip them. :)

Previously I wasn't able to rename the extension of a zip file and unzip it with FastZip (sharpziplib), as it would throw an error. I have not put in the suggestion on p1/#12 and It's working and unzipping regardless. I think I know why.
C#:
            bool resultExist = File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "result.zip"));
            if (!resultExist)
                System.IO.Compression.ZipFile.CreateFromDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "result.zip", CompressionLevel.Fastest, false);
            else Debug.WriteLine("File exists");

            bool result3mfExist = File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "result.3mf"));
            if (!result3mfExist && resultExist)
                File.Move(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "result.zip"), Path.ChangeExtension(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "result.zip"), ".3mf"));
            else Debug.WriteLine("File exists");
            //ZipFile.ExtractToDirectory("result.3mf", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "extract"));
            FastZip fZip = new FastZip();
            fZip.ExtractZip(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "result.3mf"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "extract"), null);
Uncommenting line 10 also works to unzip if I zip with FastZip instead. But I am zipping/unzipping from the same frameworks version. So now I want to troubleshoot if it works without edited the project file as I suggestion above based on your link.

Which of the attached files in p1/#8 were created not using .net or by using tar.exe? I just want to be sure by confirming this for myself.

but ZipArchive/ZipFile was added as late as .Net 4.5 (2012).
Thank you. Hmm, Microsoft listed different version dates on Nuget : System.IO.Compression 4.3.0
 
That Nuget isn't there to support .Net Framework where it's already included (just have to add reference), it's for .Net Core, .Net Standard etc.
 
Ah yea, I just noticed the in-page link I posted is for DNC. When I googled for the introduction of the compression namespace date, it was first one to pop up and I jumped to the dates.

You will need to bare with me, I'm a bit thick today... this hot weather plays havoc with my concentration!!

Which of the uploaded files on p1/#6 are the one created using ZipFile then?

I ask because I can produce zips and decompress them without the need to edit the project config and I can do it using the code I posted regardless which method zipped them up.

I was never able to unzip my older zip files before now. My old zip files were zipped long ago using SharpZipLib. And I can do it without adding this to my project file :
C#:
<runtime>
   <AppContextSwitchOverrides value="Switch.System.IO.Compression.ZipFile.UseBackslash=false" />
</runtime>

Bizarre... whereas @Stefan needs the snipped above.

Atleast the OP got a fix out of the thread and I got back into my old zips again. ;)

Thanks @JohnH.
Signing off for the night. It's been a crazy tiring day here.

Edit : Fixed my grammar for my poor illiteracy.
 
Last edited:

Latest posts

Back
Top Bottom