unit uCEFZipReader;
{$IFDEF FPC}
{$MODE OBJFPC}{$H+}
{$ENDIF}
{$I cef.inc}
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
{$MINENUMSIZE 4}
interface
uses
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes;
type
///
/// Class that supports the reading of zip archives via the zlib unzip API.
/// The functions of this interface should only be called on the thread that
/// creates the object.
///
///
/// Implements TCefZipReader
/// CEF source file: /include/capi/cef_zip_reader_capi.h (cef_zip_reader_t)
///
TCefZipReaderRef = class(TCefBaseRefCountedRef, ICefZipReader)
protected
///
/// Moves the cursor to the first file in the archive. Returns true (1) if the
/// cursor position was set successfully.
///
function MoveToFirstFile: Boolean;
///
/// Moves the cursor to the next file in the archive. Returns true (1) if the
/// cursor position was set successfully.
///
function MoveToNextFile: Boolean;
///
/// Moves the cursor to the specified file in the archive. If |caseSensitive|
/// is true (1) then the search will be case sensitive. Returns true (1) if
/// the cursor position was set successfully.
///
function MoveToFile(const fileName: ustring; caseSensitive: Boolean): Boolean;
///
/// Closes the archive. This should be called directly to ensure that cleanup
/// occurs on the correct thread.
///
function Close: Boolean;
///
/// Returns the name of the file.
///
function GetFileName: ustring;
///
/// Returns the uncompressed size of the file.
///
function GetFileSize: Int64;
///
/// Returns the last modified timestamp for the file.
///
function GetFileLastModified: TCefBaseTime;
///
/// Opens the file for reading of uncompressed data. A read password may
/// optionally be specified.
///
function OpenFile(const password: ustring): Boolean;
///
/// Closes the file.
///
function CloseFile: Boolean;
///
/// Read uncompressed file contents into the specified buffer. Returns < 0 if
/// an error occurred, 0 if at the end of file, or the number of bytes read.
///
function ReadFile(buffer: Pointer; bufferSize: NativeUInt): Integer;
///
/// Returns the current offset in the uncompressed file contents.
///
function Tell: Int64;
///
/// Returns true (1) if at end of the file contents.
///
function Eof: Boolean;
public
class function UnWrap(data: Pointer): ICefZipReader;
class function New(const stream: ICefStreamReader): ICefZipReader;
end;
implementation
uses
uCEFMiscFunctions, uCEFLibFunctions;
function TCefZipReaderRef.Close: Boolean;
begin
Result := PCefZipReader(FData)^.close(PCefZipReader(FData)) <> 0;
end;
function TCefZipReaderRef.CloseFile: Boolean;
begin
Result := PCefZipReader(FData)^.close_file(PCefZipReader(FData)) <> 0;
end;
class function TCefZipReaderRef.New(const stream: ICefStreamReader): ICefZipReader;
begin
Result := UnWrap(cef_zip_reader_create(CefGetData(stream)));
end;
function TCefZipReaderRef.Eof: Boolean;
begin
Result := PCefZipReader(FData)^.eof(PCefZipReader(FData)) <> 0;
end;
function TCefZipReaderRef.GetFileLastModified: TCefBaseTime;
begin
Result := PCefZipReader(FData)^.get_file_last_modified(PCefZipReader(FData));
end;
function TCefZipReaderRef.GetFileName: ustring;
begin
Result := CefStringFreeAndGet(PCefZipReader(FData)^.get_file_name(PCefZipReader(FData)));
end;
function TCefZipReaderRef.GetFileSize: Int64;
begin
Result := PCefZipReader(FData)^.get_file_size(PCefZipReader(FData));
end;
function TCefZipReaderRef.MoveToFile(const fileName: ustring; caseSensitive: Boolean): Boolean;
var
TempFilename : TCefString;
begin
TempFilename := CefString(fileName);
Result := PCefZipReader(FData)^.move_to_file(PCefZipReader(FData), @TempFilename, Ord(caseSensitive)) <> 0;
end;
function TCefZipReaderRef.MoveToFirstFile: Boolean;
begin
Result := PCefZipReader(FData)^.move_to_first_file(PCefZipReader(FData)) <> 0;
end;
function TCefZipReaderRef.MoveToNextFile: Boolean;
begin
Result := PCefZipReader(FData)^.move_to_next_file(PCefZipReader(FData)) <> 0;
end;
function TCefZipReaderRef.OpenFile(const password: ustring): Boolean;
var
TempPassword : TCefString;
begin
TempPassword := CefString(password);
Result := PCefZipReader(FData)^.open_file(PCefZipReader(FData), @TempPassword) <> 0;
end;
function TCefZipReaderRef.ReadFile(buffer: Pointer; bufferSize: NativeUInt): Integer;
begin
Result := PCefZipReader(FData)^.read_file(PCefZipReader(FData), buffer, buffersize);
end;
function TCefZipReaderRef.Tell: Int64;
begin
Result := PCefZipReader(FData)^.tell(PCefZipReader(FData));
end;
class function TCefZipReaderRef.UnWrap(data: Pointer): ICefZipReader;
begin
if (data <> nil) then
Result := Create(data) as ICefZipReader
else
Result := nil;
end;
end.