Creating Compressed (Zipped) Folder using Delphi

前端 未结 9 2204
日久生厌
日久生厌 2021-02-05 18:52

Can I create Windows XP\'s Compressed (Zipped) Folder using Delphi?

相关标签:
9条回答
  • 2021-02-05 19:09

    A "zipped" folder in Windows is nothing more than a .ZIP file compressed using any standard zip library. Compressed folders are a different animal and require an NTFS disk format.

    For the "Zip" file, I strongly suggest the Turbo Power Abbrevia, which is open source and works well. You might want to check this alternate site if your using Delphi 2009 as it might be a more recent copy.

    If your wanting to use the compressed folders option, you will need to modify the directory flags on the directory handle. This will only impact NEW files added to that directory and will not automatically compress existing files. If you have an existing directory you are trying to compress, then rename each existing file, and load and save it back to the original name deleting the original file when complete with each one. Yozey had a good link to the MSDN documentation. Just remember that this only works with NTFS formatted disks, so you will need to add a check for that in your code.

    0 讨论(0)
  • 2021-02-05 19:15

    You can use some command line version of any compressor like 7zip and do the task using ShellExecute, or you can use a free or comercial component like anyone of these.

    I had used ZipMaster and it behaves very well for my purpose. I don't know what are your size, space and performance requirements.

    0 讨论(0)
  • 2021-02-05 19:17

    The TZipFile.ZipDirectoryContents method did not work for me, so I created my own implementation using TZipFile.add(). I am posting it here if anyone needs it.

    procedure CreateZipOfDirectory(directory: string);
    var
      zip: TZipFile;
      Arr: tarray<string>;
      str: string;
      
      function GetAllFilesInDir(const Dir: string): tarray<string>;
      var
        Search: TSearchRec;
        procedure addAll(arr: tarray<string>; parent: string);
        var
          tmp: string;
        begin
          for tmp in arr do
          begin
            setlength(result, length(result) + 1);
            result[length(result) - 1] := IncludeTrailingBackslash(parent) + tmp;
          end;
        end;
      begin
        setlength(result, 0);
        if FindFirst(IncludeTrailingBackslash(Dir) + '*.*', faAnyFile or faDirectory, Search) = 0 then
          try
            repeat
              if (Search.Attr and faDirectory) = 0 then
              begin
                setlength(result, length(result) + 1);
                result[length(result) - 1] := Search.Name;
              end
              else if (Search.Name <> '..') and (Search.Name <> '.') then
                addAll(GetAllFilesInDir(IncludeTrailingBackslash(Dir) + Search.Name), Search.Name);
            until FindNext(Search) <> 0;
          finally
            FindClose(Search);
          end;
      end;
    
    begin
      Zip := TZipFile.Create;
      try
        Zip.Open('Foo.zip', zmWrite);
        arr := GetAllFilesInDir(directory); // The Delphi TZipFile.ZipDirectoryContents does not work properly, so let's create our own.
        for str in arr do
          zip.Add(directory + str, str); // Add the second parameter to make sure that the file structure is preserved.
      finally
        zip.Free;
      end;
    end;
    
    0 讨论(0)
提交回复
热议问题