Skip to content

Instantly share code, notes, and snippets.

@ImkeF
Last active November 19, 2019 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ImkeF/f0d587be1041fd85a6c324204163dade to your computer and use it in GitHub Desktop.
Save ImkeF/f0d587be1041fd85a6c324204163dade to your computer and use it in GitHub Desktop.
let func =
(ZIPFile_path as text) =>
let
Header = BinaryFormat.Record([
MiscHeader = BinaryFormat.Binary(14),
BinarySize = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
FileSize = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
FileNameLen= BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian),
ExtrasLen = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian)
]),
HeaderChoice = BinaryFormat.Choice(
BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
each if _ <> 67324752
then BinaryFormat.Record([IsValid = false, Filename=null, Content=null])
else BinaryFormat.Choice(
BinaryFormat.Binary(26),
each BinaryFormat.Record([
IsValid = true,
Filename = BinaryFormat.Text(Header(_)[FileNameLen]),
Extras = BinaryFormat.Text(Header(_)[ExtrasLen]),
Content = BinaryFormat.Transform(
BinaryFormat.Binary(Header(_)[BinarySize]),
(x) => try Binary.Buffer(Binary.Decompress(x, Compression.Deflate)) otherwise null
)
]),
type binary
)
),
ZipFormat = BinaryFormat.List(HeaderChoice, each _[IsValid] = true),
Entries = List.Transform(
List.RemoveLastN( ZipFormat(File.Contents(ZIPFile_path)), 1),
(e) => [FileName = e[Filename], Content = e[Content] ]
)
in
Table.FromRecords(Entries) ,
documentation = [
Documentation.Name = " File.Unzip.pq ",
Documentation.Description = " Unzips a file at a given <code>path</code>. ",
Documentation.LongDescription = " Unzips a file at a given <code>path</code>. This function is almost identical to Mark Whites: http://sql10.blogspot.de/2016/06/reading-zip-files-in-powerquery-m.html . Just that it takes the location of the zip as an input and not the file itself ",
Documentation.Category = " AccessingData ",
Documentation.Source = " www.TheBIccountant.com . ",
Documentation.Version = " 1.0 ",
Documentation.Author = " Mark White http://sql10.blogspot.de/2016/06/reading-zip-files-in-powerquery-m.html . ",
Documentation.Examples = {[Description = " ",
Code = " ",
Result = " "]}]
in
Value.ReplaceType(func, Value.ReplaceMetadata(Value.Type(func), documentation))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment