BlogAll Blog Posts | Next Post | Previous Post
Wednesday, August 5, 2020
Yesterday we introduced TWebQRCode as a free component to be used with TMS WEB Core. If you would like to work with ZIP files in a web application then we have good news for you! We created another free component: TWebZip.
TWebZip is a wrapper around the open source JSZip library. JSZip makes it possible to create, read, modify and download ZIP files directly in a client application. For limitations of the library please check the Limitations page.
Features of TWebZipThe supported data types are: string, base64 encoded string (via a parameter setting), TJSArrayBuffer and TBytes.
Drop the non-visual TWebZip component onto the form and you have an empty ZIP file already! You can also open a ZIP file, for example through a TWebFilePicker:
To add files to the ZIP, use the Add method. The first parameter is the filename with extension and the second parameter is the data to be added to the file. Keep in mind that if your ZIP already contains a file with a given name then it will override the content.
procedure TForm1.WebFilePicker1Change(Sender: TObject); begin if WebFilePicker1.Files.Count > 0 then WebFilePicker1.Files.GetAsArrayBuffer; end; procedure TForm1.WebFilePicker1GetFileAsArrayBuffer(Sender: TObject; AFileIndex: Integer; ABuffer: TJSArrayBufferRecord); begin WebZip1.Open(ABuffer); end; procedure TForm1.WebZip1ZipLoaded(Sender: TObject); begin //This is the implementation of the OnZipLoaded event: //here we know that the ZIP was successfully opened //and we can proceed with related calculations/tasks end;
To read the content of a file, use the ReadAsString, ReadAsArrayBuffer or ReadAsBytes method based on which data type you need for further processing. These methods are asynchronous. The first parameter is the filename you want to read and the second is the anonymous method for processing the data.
WebZip1.Add('subfolder/sub.txt', 'I''m in a subfolder!'); //Add sub.txt in the 'subfolder' folder WebZip1.Add('hello.txt', 'Hello World'); //Adds hello.txt to the ZIP and sets the content to 'Hello World' WebZip1.Add('hello.txt', 'Hello ZIP'); //Overrides the content of hello.txt
To remove files, use the Remove(FileName) or RemoveAll methods.
WebZip1.ReadAsString('hello.txt', procedure(AFileName, AData: string) begin //process AData here end);
Download the ZIP you created or modified with different compression levels. More information on these levels can be found here.
WebZip1.Remove('hello.txt'); //removes hello.txt from ZIP WebZip1.RemoveAll; //removes all of the files from the ZIP
Follow your download progress by implementing the OnProgress event:
WebZip1.Download('myzip.zip', zcStore); //no compression WebZip1.Download('myzip.zip', zcDeflateLevel1); //best speed WebZip1.Download('myzip.zip', zcDeflateLevel9); //best compression
There are also extra properties for: file count (TWebZip.FileCount), file names (TWebZip.FileName, TWebZip.FileNames) and file comments (TWebZip.FileComment).
procedure TForm1.WebZip1Progress(Sender: TObject; APercent: Single; ACurrentFile: string); begin WebProgressBar1.Position := Round(APercent); end;
If you would like to see how TWebZip works in practice, then head over to our demo where you can upload images (JPEG or ZIP containing JPEGs) and download their grayscale version in a ZIP file. As we mentioned at the beginning of this article, TWebZip is available for free through our TMS WEB Core Partner program and you can download it from here.
To install, open, compile & install the package from the "Component Library Source" folder. This will install the design-time TWebZip component.
For use at runtime, make sure that the "Core Source" folder is in your TMS WEB Core specific library path that you can set via IDE Tools, Options, TMS Web, Library path.
This blog post has received 5 comments.
All Blog Posts | Next Post | Previous Post