Created
February 10, 2017 11:35
-
-
Save Hugoberry/0f658c4a2f3949f6cfdf4894af55d266 to your computer and use it in GitHub Desktop.
UnZip in Power Query M
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ZIPFile) => | |
let | |
Header = BinaryFormat.Record([ Signature = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32,ByteOrder.LittleEndian), | |
Version = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16,ByteOrder.LittleEndian), | |
Flags = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16,ByteOrder.LittleEndian), | |
Compression = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16,ByteOrder.LittleEndian), | |
ModTime = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16,ByteOrder.LittleEndian), | |
ModDate = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16,ByteOrder.LittleEndian), | |
CRC32 = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32,ByteOrder.LittleEndian), | |
CompressedSize = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32,ByteOrder.LittleEndian), | |
UncompressedSize = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32,ByteOrder.LittleEndian), | |
FileNameLen = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16,ByteOrder.LittleEndian), | |
ExtraFieldLen = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16,ByteOrder.LittleEndian) | |
]), | |
FileEntry = BinaryFormat.Choice(Header, each if _[Signature] <> 0x4034B50 then BinaryFormat.Null else | |
BinaryFormat.Record([ | |
Header = _, | |
FileName = BinaryFormat.Text(_[FileNameLen]), | |
ExtraField = BinaryFormat.Text(_[ExtraFieldLen]), | |
UncompressedData = BinaryFormat.Transform(BinaryFormat.Binary(_[CompressedSize]),(x) => try Binary.Buffer(Binary.Decompress(x, Compression.Deflate)) otherwise null) | |
]), type binary), | |
ZipFormat = BinaryFormat.List(FileEntry, each _<> null), | |
Entries = List.Transform( | |
List.RemoveLastN( ZipFormat(ZIPFile), 1), | |
(e) => [FileName = e[FileName], Content = e[UncompressedData] ] | |
) | |
in | |
Table.FromRecords(Entries) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment