How to use ExtractArchive[] with a pattern that does NOT match
It looks like there's a hack that I've found to work until someone comes up with a something better.
Looking at the code for ExtractArchive[]
, this function has a pattern that takes a list of destfiles
. This wasn't obvious to me since the docs just specify that the third argument can be a 'pattern'. But given this, you can get the names of the files that you DO want first, and then give that as the third argument.
ExtractArchive["archive.tar.gz", $TemporaryDirectory,
DeleteCases[
Import["archive.tar.gz", "FileNames"], _?(StringMatchQ[#, "*.jpg"] &)
]
]
Except confuses me a bit in StringExpression, it can only be used on a Character. Maybe someone can chime in with a better answer about Except in StringExpression, but in 12.1 I can confirm this workaround works as a pattern directly to at least skip the Import:
___ ~~ "." ~~ ext___ /; (! StringMatchQ[ext, "png", IgnoreCase -> True])
Unfortunately this will not work in versions prior to 12.1 as CreateArchive and ExtractArchive were completely reworked in 12.1, and this is one of the bugs fixed. For the record, you can test this stuff out by checking if the pattern works properly in a StringMatchQ, internally ExtractArchive just passes the pattern to StringMatchQ anyway.