Zip files with the same name inside a folder

0

I wonder if there is a possibility of doing the following scenario in Delhpi:

I have a folder and inside it I have the following structure:

foto.jpg
foto.png
imagem.jpg
imagem.png

Is it possible to zip by name? so ... zip only the files that contain the same name ... resulting in:

foto.zip -> contem -> foto.jpg e foto.png
imagem.zip -> contem -> imagem.jpg e imagem.png

I do not want a ready answer, but rather the feasibility of doing this in Delphi, remembering that the files I may not know their names.

    
asked by anonymous 12.06.2018 / 21:18

2 answers

0

You can use the class System.Zip.TZipFile of the delphi itself to do this implementation, looping the files with the same name (with different extensions) and adding them in the zip according to the rule you set, follow the example based on the question:

uses
  System.SysUtils,
  StrUtils,
  Classes,
  System.Zip;

var
  vArquivos: TStringList;
  vDiretorio: String;
  vDiretorioSaida: String;
  searchResult : TSearchRec;
  vArquivoSemExtensao: String;
  I: Integer;
  ZipFile: TZipFile;
begin
  vDiretorio := 'D:\Arquivos';
  vDiretorioSaida := 'D:\ArquivosZipado';
  vArquivos := TStringList.Create;
  try
    if findfirst(vDiretorio+'\*.*', faAnyFile, searchResult) = 0 then
    begin
      repeat
        vArquivos.Sort;
        if (searchResult.Name <> '.') and (searchResult.Name <> '..') then
        begin
          vArquivoSemExtensao := ChangeFileExt(searchResult.Name, '');
          if not vArquivos.Find(vArquivoSemExtensao, I) Then
            vArquivos.Add(vArquivoSemExtensao);
        end;
      until FindNext(searchResult) <> 0;
    end;
    FindClose(searchResult);

    for I := 0 to vArquivos.Count -1 do
    begin
      ZipFile := TZipFile.Create;
      try
        ZipFile.Open(vDiretorioSaida+'\'+vArquivos.Strings[I]+'.zip', zmWrite);
        if findfirst(vDiretorio+'\'+vArquivos.Strings[I]+'.*', faAnyFile, searchResult) = 0 then
        begin
          repeat
            ZipFile.add(vDiretorio+'\'+searchResult.Name);
          until (FindNext(searchResult) <> 0);
        end;
        FindClose(searchResult);
      finally
        FreeAndNil(ZipFile);
      end;
    end;
  finally
    FreeAndNil(vArquivos);
  end;
end.
    
12.06.2018 / 22:23
-1

Yes it is possible, in case I used WinRAR but with the zip it works also

"C:\Program Files (x86)\WinRAR\WinRAR.exe" a "E:\Backup\foto.rar" "E:\Backup\foto.*" 

If you use the "*" instead of the extension, all files with the name "photo" independent of the extension will be compressed ...

If you have any questions, you can take a look at here >

    
12.06.2018 / 21:38