Advertisement
maranite

PowerQuery_Unzip

Jun 1st, 2016
43,285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ZIPFile) =>
  2. let
  3.     Header = BinaryFormat.Record([
  4.         MiscHeader = BinaryFormat.Binary(14),
  5.         BinarySize = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
  6.         FileSize   = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
  7.         FileNameLen= BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian),
  8.         ExtrasLen  = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian)    
  9.     ]),
  10.  
  11.     HeaderChoice = BinaryFormat.Choice(
  12.         BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
  13.         each if _ <> 67324752             // not the IsValid number? then return a dummy formatter
  14.             then BinaryFormat.Record([IsValid = false, Filename=null, Content=null])
  15.             else BinaryFormat.Choice(
  16.                     BinaryFormat.Binary(26),      // Header payload - 14+4+4+2+2
  17.                     each BinaryFormat.Record([
  18.                         IsValid  = true,
  19.                         Filename = BinaryFormat.Text(Header(_)[FileNameLen]),
  20.                         Extras   = BinaryFormat.Text(Header(_)[ExtrasLen]),
  21.                         Content  = BinaryFormat.Transform(
  22.                             BinaryFormat.Binary(Header(_)[BinarySize]),
  23.                             (x) => try Binary.Buffer(Binary.Decompress(x, Compression.Deflate)) otherwise null
  24.                         )
  25.                         ]),
  26.                         type binary                   // enable streaming
  27.                 )
  28.     ),
  29.  
  30.     ZipFormat = BinaryFormat.List(HeaderChoice, each _[IsValid] = true),
  31.  
  32.     Entries = List.Transform(
  33.         List.RemoveLastN( ZipFormat(ZIPFile), 1),
  34.         (e) => [FileName = e[Filename], Content = e[Content] ]
  35.     )
  36. in
  37.     Table.FromRecords(Entries)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement