3

I have a ZIP archive that does not have the .zip file extension. How can I import a file from this archive using Import and specifying a particular import format or element?


Test case:

SetDirectory@CreateDirectory[]
x = 1;
Export["x.m", HoldComplete[x], "HeldExpressions"]
CreateArchive["x.m"]
RenameFile["x.m.zip", "a.foo"]

Now I can

Import["a.foo", {"ZIP", "x.m"}]
(* 1 *)

But I want to import x.m as "HeldExpressions". How can I do this? I cannot figure out the syntax.


Doing this is useful because lots of file formats are based on zip, e.g. Java JAR files, Microsoft Office files, .paclet files, etc. Suppose I want to import an XML from a .docx or I want to import a PacletInfo.m as "HeldExpressions".

I know I can ExtractArchive first, import, and delete the extracted file. This question is about whether this is achievable with a single Import call.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 1
    let me guess what you are just trying :-). Have you tried Import["a.foo", {"ZIP", "x.m", "HeldExpressions"}], that seems to work for me... It even is kind of documented in the details section of Import (in the part "assume also compression formats...") – Albert Retey Dec 03 '16 at 10:22
  • @AlbertRetey Please post an answer ... I really couldn't find it – Szabolcs Dec 03 '16 at 10:35
  • 1
    Related, perhaps duplicate?: http://mathematica.stackexchange.com/q/2871/121 – Mr.Wizard Dec 03 '16 at 10:43

1 Answers1

6

The following seems to work:

 Import["a.foo", {"ZIP", "x.m", "HeldExpressions"}]

the documentation is not really clear about it, but the explanation about compressed formats in the Details section of the Import documentation page can be read as if this should work.

Albert Retey
  • 23,585
  • 60
  • 104