HamiD
HamiD

Reputation: 197

Read/Write file from a .zip archive

Should I work with files within the archive file. (Read - write). By the following code, I get a list of files on my needs.

Zip := TZipFile.Create;
  try
    Zip.Open(FilePath, TZipMode.zmRead);
    For File_Name in Zip.FileNames do
    begin
      //some code
    end;
  finally
    Zip.Close;
    FreeAndNil(Zip);
  end;

I used the TZipFile.Read method to reads a file from a .zip archive .

This method returns the complete content of file into a buffer of type TByte. But just need to read a 1MB file from the beginning, not the complete file.

After reading and analyzing a 1MB file, if needed, should be read complete file and make changes to the file and re-save the file to archive.

Memory and speed of the program is very important. I used to set the buffer size of the function SetLength, unfortunately complete content of file files are stored in the buffer.

What do you think?

Upvotes: 1

Views: 2326

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597215

Use the overloaded version of TZipFile.Read() that returns a TStream instead of a TBytes. That way, you do not have to read the entire file into memory, and can read just its beginning bytes as needed.

Unfortunately, there is no way to modify data inside of a zip archive using TZipFile. Although you can Extract() a particular file, modify it externally as needed, and then Add() it back into TZipFile, there is no way to remove/replace a given file in TZipFile. TZipFile is a simple framework, it can only read a zip archive and add new files to it, nothing else. If you need more control over a zip archive, you are better off using a more complete third-party solution, such as ZipForge.

Upvotes: 2

Related Questions