procedure kompresFile(Files : TStrings; const namaFile : String); var fileMasuk, fileKeluar, tmpFile : TFileStream; kompres : TCompressionStream; i,l : Integer; s : String; begin if Files.Count > 0 then begin fileKeluar := TFileStream.Create(namaFile,fmCreate); try { jumlah file yang akan di kompres } l := Files.Count; fileKeluar.Write(l,SizeOf(l)); for i := 0 to Files.Count-1 do begin fileMasuk := TFileStream.Create(Files[i],fmOpenRead); try { nama asli file } s := ExtractFilename(Files[i]); l := Length(s); fileKeluar.Write(l,SizeOf(l)); fileKeluar.Write(s[1],l); { ukuran asli file } l := fileMasuk.Size; fileKeluar.Write(l,SizeOf(l)); { kompress dan simpan file temporari} tmpFile := TFileStream.Create('tmp',fmCreate); kompres := TCompressionStream.Create(clMax,tmpfile); try kompres.CopyFrom(fileMasuk,l); finally kompres.Free; tmpFile.Free; end; { menambahkan file terkompresi ke file tujuan } tmpFile := TFileStream.Create('tmp',fmOpenRead); try fileKeluar.CopyFrom(tmpFile,0); finally tmpFile.Free; end; finally fileMasuk.Free; end; end; finally fileKeluar.Free; end; DeleteFile('tmp'); end; end;
procedure TForm1.button1Click(Sender: TObject); begin kompresFile(namaBeberapaFileYangAkanDiKompres , namaOutputFileTerkompres); end;
procedure dekompresFile(const namaFile, folderTujuan : String); var tujuan,s : String; dekompres : TDecompressionStream; fileMasuk, fileKeluar : TFilestream; i,l,c : Integer; begin tujuan := IncludeTrailingPathDelimiter(folderTujuan); fileMasuk := TFileStream.Create(namaFile,fmOpenRead); try { number of files } fileMasuk.Read(c,SizeOf(c)); for i := 1 to c do begin { membaca nama file } fileMasuk.Read(l,SizeOf(l)); SetLength(s,l); fileMasuk.Read(s[1],l); { membaca ukuran file } fileMasuk.Read(l,SizeOf(l)); { dekompres file dan menyimpannya } s := tujuan+s; //memasukan tujuan pathnya fileKeluar := TFileStream.Create(s,fmCreate); dekompres := TDecompressionStream.Create(fileMasuk); try fileKeluar.CopyFrom(dekompres,l); finally fileKeluar.Free; dekompres.Free; end; end; finally fileMasuk.Free; end; end;
procedure TForm1.button1Click(Sender: TObject); begin dekompresFile(fileYangAkanDiDekompres,folderTujuan); end;
4 komentar:
algoritma yang digunakan .... algoritmanya apa gan ????
mohon balasannya gan.......
sedot langsung
Kalau ambil folder gmn gan?
Posting Komentar