Created
May 24, 2022 18:42
-
-
Save rogeriopradoj/bdb6069605304fb89ec0d1c54eb1bdf8 to your computer and use it in GitHub Desktop.
Power Query M Formula Language - DecompressZip - function (ZIPFile as any) as any
This file contains 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) => | |
// expects full path to the ZIP file, only extracts the first data file after getting its size from the central directory | |
// https://en.wikipedia.org/wiki/Zip_(file_format)#Structure | |
let | |
//read the entire ZIP file into memory - we'll use it often so this is worth it | |
Source = Binary.Buffer(File.Contents(ZIPFile)), | |
// get the full size of the ZIP file | |
Size = Binary.Length(Source), | |
//Find the start of the central directory at the sixth to last byte | |
Directory = BinaryFormat.Record([ | |
MiscHeader=BinaryFormat.Binary(Size-6), | |
Start=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian) | |
]) , | |
Start = Directory(Source)[Start], | |
//find the first entry in the directory and get the compressed file size | |
FirstDirectoryEntry = BinaryFormat.Record([ | |
MiscHeader=BinaryFormat.Binary(Start+20), | |
FileSize=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian), | |
UnCompressedFileSize=BinaryFormat.Binary(4), | |
FileNameLen=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian), | |
ExtrasLen=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian) | |
]) , | |
//figure out where the raw data starts | |
Offset = 30+FirstDirectoryEntry(Source)[FileNameLen]+FirstDirectoryEntry(Source)[ExtrasLen], | |
Compressed = FirstDirectoryEntry(Source)[FileSize]+1, | |
//get the raw data of the compressed file | |
Raw = BinaryFormat.Record([ | |
Header=BinaryFormat.Binary(Offset), | |
Data=BinaryFormat.Binary(Compressed) | |
]) | |
// unzip it | |
in | |
Binary.Decompress(Raw(Source)[Data], Compression.Deflate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment