Friday, 17 May 2019

Reading .zip files in Delphi and Python

Metasequoia uses a new default file format from version 4.7 which has extension *.mqoz.
This a just a *.zip file that contains an *.mqo file and a thumbnail.jpg file and maybe an *.mqx file.

Below are examples in Delphi 10.2 and Python 2.7 that show how to read the *.mqo inside the *.zip.



//Delphi 10.2
uses System.Zip;
procedure TForm1.FormCreate(Sender: TObject);
var
zipfile :TZipFile;
i : integer;
s : string;
found : Boolean;
m : TStream;
hdr:TZipHeader;
begin
Memo1.Clear;
found:=False;
if TZipFile.IsValid('C:/Users/Username/Desktop/test.mqoz') then
begin
try
zipfile:=TZipFile.Create;
zipfile.Open('C:/Users/Username/Desktop/test.mqoz',zmRead);
for i:= Low(zipfile.FileNames) to High(zipfile.FileNames) do
begin
s:= zipfile.FileNames[i];
if LowerCase(ExtractFileExt(s))='.mqo' then
begin
found:=True;
break;
end;
end;
if found then
begin
m:=TStream.Create;
try
zipfile.Read(i,m,hdr);
memo1.Lines.LoadFromStream(m);
finally
m.Free;
end;
end;
finally
zipfile.Close;
zipfile.Free;
end;
end;
end;
view raw zipfile.pas hosted with ❤ by GitHub


#Python 2.7
import zipfile
import os
def read_zip_file(filepath):
zfile = zipfile.ZipFile(filepath)
for finfo in zfile.infolist():
print finfo.filename
f,ext = os.path.splitext(finfo.filename)
if ext.lower()== ".mqo":
ifile = zfile.open(finfo)
line_list = ifile.readlines()
print line_list
if __name__=="__main__":
read_zip_file("test.mqoz")
view raw zipreader.py hosted with ❤ by GitHub


No comments:

Post a Comment