3
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ 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...") $\endgroup$ Dec 3, 2016 at 10:22
  • $\begingroup$ @AlbertRetey Please post an answer ... I really couldn't find it $\endgroup$
    – Szabolcs
    Dec 3, 2016 at 10:35
  • 1
    $\begingroup$ Related, perhaps duplicate?: mathematica.stackexchange.com/q/2871/121 $\endgroup$
    – Mr.Wizard
    Dec 3, 2016 at 10:43

1 Answer 1

5
$\begingroup$

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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.