Show: Delphi C++
Display Preferences

System.Zip.TZipFile.Extract

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

procedure Extract(const FileName: string; const Path: string = ''; CreateSubdirs: Boolean = True); overload;
procedure Extract(Index: Integer; const Path: string = ''; CreateSubdirs: Boolean = True); overload;

C++

void __fastcall Extract(const System::UnicodeString FileName, const System::UnicodeString Path = System::UnicodeString(), bool CreateSubdirs = true)/* overload */;
void __fastcall Extract(int Index, const System::UnicodeString Path = System::UnicodeString(), bool CreateSubdirs = true)/* overload */;

Properties

Type Visibility Source Unit Parent
procedure
function
public
System.Zip.pas
System.Zip.hpp
System.Zip TZipFile

Description

Extracts a single file from the .zip archive.

  • FileName specifies the file to be extracted from the .zip file. All slashes in the .zip file names should be '/'.
  • Index represents the index of this file in the .zip archive.
  • Path represents the location, on the disk, where the file will be extracted.
  • CreateSubDirs indicates whether the output should create subdirectories specified in the .zip file. The default value for CreateSubDirs is True.

The second overload procedure is useful when a .zip file has duplicate file names.

To track the progress of the extraction, use OnProgress.

Extracting Password-protected Files

In order to extract password-protected (encrypted) files, you can use the OnCreateDecompressStream event or the CreateDecompressStreamCallBack callback. The following example demonstrates how to use OnCreateDecompressStream to extract an encrypted file:

  1. Assign the OnCreateDecompressStream event:

    Delphi:

    ...
      myZipFile := TZipFile.Create;
      myZipFile.OnCreateDecompressStream := OnCreateDecompressStream;
      try
        myZipFile.ExtractAll('');
        myZipFile.Close;
      finally
        myZipFile.Free;
      end;
    ...

    C++:

    ...
    	std::unique_ptr<TZipFile> myZipFile(new TZipFile);
    	myZipFile->OnCreateDecompressStream = OnCreateDecompressStream;
    	myZipFile->ExtractAll("../../");
    	myZipFile->Close();
    ...
  2. Perform decryption in your OnCreateDecompressStream function:

    Delphi:

    • Include the definition in the private section:
        private
          { Private declarations }
          function OnCreateDecompressStream(const AInStream: TStream; const AZipFile: TZipFile;
            const AHeader: TZipHeader; AIsEncrypted: Boolean): TStream;
    • Implement the OnCreateDecompressStream function:
      function TForm1.OnCreateDecompressStream(const AInStream: TStream; const AZipFile: TZipFile;
        const AHeader: TZipHeader; AIsEncrypted: Boolean): TStream;
      begin
        try
          if AIsEncrypted then
          begin
            // Perform decrypt operation on AInStream. For example, you can use your own implementation of CryptZip or AES-256. 
            // Result := DecryptedStream;
          end
          else
             Result := AInStream;
        except
          on E:Exception do
          begin
            Result := AInStream;
          end;
        end;
      end;
  3. C++:

    • Include the definition in you .h file:
      private:	// User declarations
      	TStream* __fastcall OnCreateDecompressStream (TStream* const InStream, TZipFile* const ZipFile, const TZipHeader &Item, bool IsEncrypted);
    • Implement the OnCreateDecompressStream function:
      TStream* __fastcall TForm2::OnCreateDecompressStream(TStream* const InStream, TZipFile* const ZipFile, const TZipHeader &Item, bool IsEncrypted)
      {
      	try
      	{
      		if (IsEncrypted)
      		{
      			// Perform decrypt operation on AInStream. For example, you can use your own implementation of CryptZip or AES-256.
      			// return DecryptedStream;
      		}
      		else
      		{
      			return InStream;
      		}
      	}
      	catch (Exception &e)
      	{
      		return InStream;
      	}
      }

See Also

Personal tools
In Other Languages
Previous Versions
Assistance