我现在需要用程序自动把一个目录的文件全部压缩成winrar类型文件,这样可以大减少文件的大小,然我需要用程序自动把这个压缩文件自动解压到指定的目录,请各位帮帮忙,提提意见,最好给点源程序.

解决方案 »

  1.   

    压缩成winrar好像不行,zip格式还差不多。
    但我也不会,呵呵
      

  2.   

    http://www.delphi.club.tw/viewtopic.php?p=8439function MyCompressThisDirToRARFileok ( ThisDir ,
                            DestRarFileName : string ;
                            CompressMode : string = '' ) : Boolean ; stdcall ;
            var
                SourceDir                 : string ;
                WinRAREXE                 : string ;
            begin        try
                Result := False ;
                SourceDir := GetCurrentDir ;
                if MyGetToolRAROK ( WinRAREXE ) then
                    exit
                else if Not FileExistS ( WinRAREXE ) then
                    begin
                    File_MsgBox ( '壓縮工具﹝RAR﹞不存在,壓縮命令取消。' ) ;
                    exit ;
                    end ;
                if FileExists ( DestRarFileName ) then
                    if Not DeleteFileSOK ( DestRarFileName ) then
                        MsgBox ( '標的檔﹝' + DestRarFileName +
                                      '﹞已存在,但刪除失敗。' ) ;
                if Not DirectoryExistS ( ThisDir ) then
                    begin
                    My_MsgBox ( ThisDir +
                                  ' Not Exist , Compress Command canceled.' ) ;
                    exit ;
                    end ;            if Not SetCurrentDir ( ThisDir ) then
                    exit ;            if MyExecute ( WinRAREXE + ' a -r ' + DestRarFileName + ' *.*' ,
                                        True , True , nil ) then
                    begin
    //                My_MsgBox ( '壓縮成功。' ) ;
                    Result := FileExistS ( DestRarFileName ) ;
                    end
                else
                    begin
    //                My_MsgBox ( '壓縮失敗。' ) ;
                    end ;
            finally
                SetCurrentDir ( SourceDir ) ;
            end ;
            end ;
      

  3.   

    MyCompressThisDirToRARFileok,这个函数是怎么定义,到那里去找啊,谢谢大侠
      

  4.   

    MyGetToolRAROK 
    DeleteFileSOK 
    这些函数是什么啊
    那位老兄能不能详细点呢
      

  5.   

    用程序调用winexec()启动rar控制台,前题是必须在安装了rar的机器上运行以下抄自"控制台RAR中文手册"-----------------------------------------------
    RAR 命令行语法
     ~~~~~~~~~~~~~~ 语法    RAR <命令>  [ -<开关> ]  <压缩文件>  [ <@列表文件...> ]
            [ <文件...> ]  [ <解压路径\> ] 描述    命令行选项 (命令和开关) 提供了使用 RAR 创建和管理压缩文件的控制方法。命
        令是一个字符串(或单个的字母),命令 RAR 去执行一个相应的操作。开关被用来
        改变 RAR 执行操作的方法。其它参数是压缩文件名和被压缩的文件或要从压缩文件
        中被解压文件。    列表文件是一个包括处理的文件名的纯文本文件。第一列应该以文件名开始。可以
        在//字符后添加注释。例如,你可以创建包含下列字符串的 backup.lst:    c:\work\doc\*.txt         //备份文本文档
        c:\work\image\*.bmp       //备份图片
        c:\work\misc    接着运行:       rar a backup @backup.lst    如果你希望从标准输入设备读取文件名,指定空的文件列表名(只有@)。    在列表文件中 Win32 控制台 RAR 使用 OEM (DOS) 编码。    你可以在同一命令行指定普通文件名和列表文件。如果文件和列表
        文件都未被指定,那么 RAR 将默认是 *.*,来处理所有文件    在一个 UNIX 环境变量中,你需要将通配符置于引号中,避免被外壳扩展。例如,
        这个命令将从当前路径中的 RAR 压缩文件解压所有的 *.asm 文件:       rar e '*.rar' '*.asm'
        命令可以是下列中的任何一个:(略)
      

  6.   

    谢谢,这个方法我知道,但是压缩起来就是没有程序直接方便,而且不易出错,我现在用一个组件,但是压缩的效率没有WinRar高,没有更好的组件咯.
      

  7.   

    要是压缩成zip我倒知道,
    Delphi自带了zip压缩的库ZLib,
    uses一下就可以了rar格式就不清楚了,不过winrar好象也支持zip格式啊
      

  8.   

    用VCLZIP组件压缩不错
    strSourcePath 源路径
    strFilePath  压缩结果路径VclZip: TVclZip;    if strSourcePath = '' then Exit;
        VclZip.ZipName := strFilePath  +'a.Rar';
        VclZip.RootDir := strSourcePath;
        VclZip.FilesList.Add(strSourcePath + '*.*');
        VclZip.Recurse := True;
        VclZip.RelativePaths := True;
        VclZip.DoAll := True;
        VclZip.OverwriteMode := AlWays;
        VclZip.RetainAttributes := True;
        VclZip.IncludeHiddenFiles := True;    VclZip.Zip;
    解压
    strSourcePath备份文件路径
    strFilePath解压路径
    VclUnZip: TVclUnZip;    if Trim(strSourcePath) ='' then raise Exception.Create('备份文件丢失!');
        VclUnZip.ZipName := strSourcePath +  'a.rar';
        VclUnZip.DestDir := strFilePath;
        VclUnZip.RecreateDirs := True;
        VclUnZip.RetainAttributes := True;
        VclUnZip.OverwriteMode := AlWays;
        VclUnZip.DoAll := True;
        VclUnZip.UnZip;
      

  9.   

    用VCLZIP组件我就用这的这个啊,但是效果没有WinRAR的效果好呢.要差一些啊,没有压缩成rar的组件咯
    那位大侠知道.
      

  10.   

    那只是文件可以是这样啊,但并不能达到RAR的效果啊.
      

  11.   

    你看我直接用WinRAR压缩一些文件,压缩后的大小大约只有73K左右,而通过VCLZIP组件压缩后,大约却有100K左右.这是为什么呢.难道没有与WinRAR一样的效果的组件或方法吗.
      

  12.   

    下面这个给你当成参考:是压缩与解压的原理
    procedure StreamCompress( InPutStream, OutputStream: Tstream);
       var
         comStream:TCompressionStream;
         Buffer:array[0..BufferSize] of char;
         i:integer;
    begin
     if not(assigned(InputStream) and assigned(OutputStream)) then exit;
      try
         comStream:=TCompressionStream.Create(clMax,OutPutStream);
          for i:=1 to InputStream.Size div Buffersize do
          begin
             InPutStream.ReadBuffer(buffer,BufferSize);
             comStream.WriteBuffer(buffer,BufferSize);
          end;
          i:= inputStream.Size  mod BufferSize;
          if i<>0 then
            begin
              inputStream.ReadBuffer(Buffer,i);
              comStream.WriteBuffer(buffer,i);
             end;
      finally
        FreeAndNil(comStream);
      end;
    end;procedure UnCompressit( CompressedStream: TMemoryStream;  UnCompressedStream: TMemoryStream);
    //参数压缩过的流,解压后的流
    var
      SourceStream: TDecompressionStream;
      DestStream: TMemoryStream;
      Buffer: PChar;
      Count: int64;
    begin
      CompressedStream.ReadBuffer(buffer, SizeOf(Count));//从被压缩的图像流中读出原始的尺寸
      GetMem(Buffer, Count);//根据尺寸大小为将要读入的原始流分配内存块
      DestStream := TMemoryStream.Create;
      SourceStream := TDecompressionStream.Create(CompressedStream);
      try
        SourceStream.ReadBuffer(Buffer^, Count);//将被压缩的流解压缩,然后存入 Buffer内存块中
        DestStream.WriteBuffer(Buffer^, Count);//将原始流保存至 DestStream流中
        DestStream.Position := 0; //复位流指针
        //DestStream.Position := length(VER_INFO);
        UnCompressedStream.LoadFromStream(DestStream);//从 DestStream流中载入图像流
      finally
        FreeMem(Buffer);
        DestStream.Free;
      end;
    end;
      

  13.   

    各位楼主,就真的没有WINRar压缩的组件吗
      

  14.   

    各位楼主,就真的没有WINRar压缩的组件吗
      

  15.   

    楼主看了没有啊,自己做一个压缩与解压是效果没有直接WinRAR好啊,我用过许多方法,问题我是解决了,
    你看我直接用WinRAR压缩一些文件,压缩后的大小大约只有73K左右,而通过VCLZIP组件压缩后,大约却有100K左右.这是为什么呢.难道没有与WinRAR一样的效果的组件或方法吗.
      

  16.   

    用vclzip,如过没有,给我发个消息,我传给你[email protected]
      

  17.   

    我用得就是vclzip,但是效率没有直接用winrar高,要低一些.
      

  18.   

    自已用程序写一个了!
    uses Zlib
    //数据压缩
    procedure CompressFiles(Files : TStrings; const Filename : String);
    var
      infile, outfile, tmpFile : TFileStream;
      compr : TCompressionStream;
      i,l : Integer;
      s : String;
    begin
      if Files.Count > 0 then
      begin
        outFile := TFileStream.Create(Filename,fmCreate);
        try
          { the number of files }
          l := Files.Count;
          outfile.Write(l,SizeOf(l));
          for i := 0 to Files.Count-1 do
          begin
            infile := TFileStream.Create(Files[i],fmOpenRead);
            try
              { the original filename }
              s := ExtractFilename(Files[i]);
              l := Length(s);
              outfile.Write(l,SizeOf(l));
              outfile.Write(s[1],l);
              { the original filesize }
              l := infile.Size;
              outfile.Write(l,SizeOf(l));
              { compress and store the file temporary}
              tmpFile := TFileStream.Create('tmp',fmCreate);
              compr := TCompressionStream.Create(clMax,tmpfile);
              try
                compr.CopyFrom(infile,l);
              finally
                compr.Free;
                tmpFile.Free;
              end;
              { append the compressed file to the destination file }
              tmpFile := TFileStream.Create('tmp',fmOpenRead);
              try
                outfile.CopyFrom(tmpFile,0);
              finally
                tmpFile.Free;
              end;
            finally
              infile.Free;
            end;
          end;
        finally
          outfile.Free;
        end;
        DeleteFile('tmp');
      end;
    end;//数据反压缩
    procedure DecompressFiles(const Filename, DestDirectory : String);
    var
      dest,s : String;
      decompr : TDecompressionStream;
      infile, outfile : TFilestream;
      i,l,c : Integer;
    begin
      // IncludeTrailingPathDelimiter (D6/D7 only)
      dest := IncludeTrailingPathDelimiter(DestDirectory);
      infile := TFileStream.Create(Filename,fmOpenRead);
      try
        { number of files }
        infile.Read(c,SizeOf(c));
        for i := 1 to c do
        begin
          { read filename }
          infile.Read(l,SizeOf(l));
          SetLength(s,l);
          infile.Read(s[1],l);
          { read filesize }
          infile.Read(l,SizeOf(l));
          { decompress the files and store it }
          s := dest+s; //include the path
          outfile := TFileStream.Create(s,fmCreate);
          decompr := TDecompressionStream.Create(infile);
          try
            outfile.CopyFrom(decompr,l);
          finally
            outfile.Free;
            decompr.Free;
          end;
        end;
      finally
        infile.Free;
      end;
    end;