Problems writing 3mf files - ZipArchive compression

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
headblast
Posts: 3
Joined: Tue Jun 01, 2021 8:28 am

Problems writing 3mf files - ZipArchive compression

Post by headblast »

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 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 unpacked for testing my code and here is the problem:
When I have all the files and folder-structure ready and try to pack them into an archive I run into errors:

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 https://dev.3mf.hp3d.io/ and it throws:

FATAL_ERROR [Missing required file _rels/.rels]

the file is in the archive and is in the right place but for some reason won't be found by the interpreter.

Is there someone that can point me in the right direction?
Maybe zip files have different ways to store path information that is not visible to me as the user?
Attachments
exporttest.zip
(1.3 KiB) Downloaded 23 times
headblast
Posts: 3
Joined: Tue Jun 01, 2021 8:28 am

Re: Problems writing 3mf files - ZipArchive compression

Post by headblast »

as a followup here I have two examples whch to me look pretty much identical.
One made by using the windows UI, the other one with tar.
One throws the ‘missing file’-error in the hp validator, the other one doesn’t.
Attachments
valid.zip
(1.03 KiB) Downloaded 26 times
invalid.zip
(1.03 KiB) Downloaded 23 times
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Problems writing 3mf files - ZipArchive compression

Post by onekk »

Maybe the zip format are different.

usually zip file in windows are PKZip file format

tar.exe I suppose is using the unix gzip format and maybe C# libs use the gzip format too.

Maybe the header will tell you something, usually the first characters of a compressed archive are ascii signature that describe the format PKZ something if I rember well is the signature for Windows PKzip format.

this is a common mistake from the user that come from windows in an unix environment.

see:

https://en.wikipedia.org/wiki/Gzip

Hope it helps

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
headblast
Posts: 3
Joined: Tue Jun 01, 2021 8:28 am

Re: Problems writing 3mf files - ZipArchive compression

Post by headblast »

I actually think I found the solution.
3mf expects “/” as the path separator in the file while every Zip-Library known to mankind uses “\” by default.

So when I use ZipArchive to create every file myself everything works out

Code: Select all

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();
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Problems writing 3mf files - ZipArchive compression

Post by onekk »

Oh, not an exotic problem, the old POSIX vs Windows style path separator.

Maybe there is a way to make libraries to output POSIX style path.

Sorry for bothering.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
jonasb
Posts: 162
Joined: Tue Dec 22, 2020 7:57 pm

Re: Problems writing 3mf files - ZipArchive compression

Post by jonasb »

headblast wrote: Tue Jun 01, 2021 4:22 pm ... while every Zip-Library known to mankind uses “\” by default.
Even though the (best that we have) zip spec clearly states in §4.4.17.1 "All slashes MUST be forward slashes" :roll:

So the larger problem seem to be that only Windows is known to mankind ;-) (sorry, could not resist)
onekk wrote: Tue Jun 01, 2021 5:01 pm Oh, not an exotic problem, the old POSIX vs Windows style path separator.
In this specific case it's more URI vs. Windows (but I won't argue against URI's POSIX heritage)

3MF is based on the Open Packaging Conventions (OPC, for short), that happen to address their parts via URIs. And those use forward slashes.
headblast wrote: Tue Jun 01, 2021 8:31 am If I use c# ZipFile or sharpziplib to zip the files - the .3mf throws an error.
In .NET you have the luxury of having an OPC API provided out of the box: System.IO.Packaging. This is much better suited to explore a 3MF file than ZipFile.

Regarding 3MF support for FreeCAD, you may want to look for a Python OPC Lib to build upon. This could save you lots of hassle with the lower layer.
Post Reply