 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Avatar Viper Guest
|
Posted: Tue Jan 04, 2005 6:59 am Post subject: How to use ZLib to zip up a folder in Delphi 2005??? |
|
|
How to use ZLib to zip up a folder in Delphi 2005. Any code axample to
show me. I cannot find any tutorial on the net. Thx.
|
|
| Back to top |
|
 |
John E. Doe Guest
|
Posted: Tue Jan 04, 2005 9:26 am Post subject: Re: How to use ZLib to zip up a folder in Delphi 2005??? |
|
|
On 3 Jan 2005 22:59:39 -0800, [email]chuayongquan (AT) hotmail (DOT) com[/email] (Avatar Viper)
wrote:
| Quote: | How to use ZLib to zip up a folder in Delphi 2005. Any code axample to
show me. I cannot find any tutorial on the net. Thx.
|
Just wondering..
Did you spend more than 5 seconds looking?
|
|
| Back to top |
|
 |
chuayongquan@hotmail.com Guest
|
Posted: Wed Jan 05, 2005 2:02 am Post subject: Re: How to use ZLib to zip up a folder in Delphi 2005??? |
|
|
John E. Doe wrote:
| Quote: | On 3 Jan 2005 22:59:39 -0800, [email]chuayongquan (AT) hotmail (DOT) com[/email] (Avatar Viper)
wrote:
How to use ZLib to zip up a folder in Delphi 2005. Any code axample
to
show me. I cannot find any tutorial on the net. Thx.
Just wondering..
Did you spend more than 5 seconds looking?
|
Actually i have spend 2 days searching for it, but all the codes cannot
work in delphi 2005.
|
|
| Back to top |
|
 |
Maarten Wiltink Guest
|
Posted: Wed Jan 05, 2005 7:23 am Post subject: Re: How to use ZLib to zip up a folder in Delphi 2005??? |
|
|
<chuayongquan (AT) hotmail (DOT) com> wrote
| Quote: | John E. Doe wrote:
On 3 Jan 2005 22:59:39 -0800, [email]chuayongquan (AT) hotmail (DOT) com[/email] (Avatar Viper)
wrote:
How to use ZLib to zip up a folder in Delphi 2005. Any code axample
to show me. I cannot find any tutorial on the net. Thx.
Just wondering..
Did you spend more than 5 seconds looking?
Actually i have spend 2 days searching for it, but all the codes cannot
work in delphi 2005.
|
IIUC, Delphi 2005 can compile for win32 and for .Net. Existing code for
ZLib should work for its platform with at worst minor modifications.
You do need to be prepared to make those minor modifications, and to
work on that yourself. If you have any SPECIFIC!! questions with the
code and compiler error messages that precipitated them, feel free to
ask here.
(I'm not trying to insult your intelligence, but "I compil these code
and it doesnt work" is not a specific question. Which didn't stop
other people from posting it, although it should have.)
Groetjes,
Maarten Wiltink
|
|
| Back to top |
|
 |
chuayongquan@hotmail.com Guest
|
Posted: Thu Jan 06, 2005 7:11 am Post subject: Re: How to use ZLib to zip up a folder in Delphi 2005??? |
|
|
I have figure out how to compress a file already. But can someone tell
me the logic of compressing a folder.
|
|
| Back to top |
|
 |
Maarten Wiltink Guest
|
Posted: Thu Jan 06, 2005 9:01 am Post subject: Re: How to use ZLib to zip up a folder in Delphi 2005??? |
|
|
<chuayongquan (AT) hotmail (DOT) com> wrote
| Quote: | I have figure out how to compress a file already. But can someone
tell me the logic of compressing a folder.
|
Define a meta-structure that looks like a file (easy, because every
bytestring does) but completely describes a directory and all the
files in it. Filesystems do this, but they generally describe the
files by reference, whereas you might want to include it directly.
For example, start with a "directory" consisting of "entries" that
each describe one file: name, attributes, size. You can make the
entries fixed-size or prefix them with their length. The directory
can also be prefixed with its length (in entries) or be terminated
with an invalid entry. One with a zero length should work or in the
case of fixed-length entries, one with an invalid filename.
Then append the contents of all the files.
Depending on the mechanics of your compression, you can either
construct the whole file first (possibly on the fly) or construct
only the directory, compress it, and append each file pre-compressed.
Read a book on filesystems. That should tell you how they use a
linear storage device to store a hierarchy of directories and files,
which is essentially what you want.
Groetjes,
Maarten Wiltink
|
|
| Back to top |
|
 |
chuayongquan@hotmail.com Guest
|
Posted: Fri Jan 07, 2005 4:42 am Post subject: Re: How to use ZLib to zip up a folder in Delphi 2005??? |
|
|
If there are some code samples to show me, that would be great!!!
|
|
| Back to top |
|
 |
Michael Brown Guest
|
Posted: Fri Jan 07, 2005 8:28 am Post subject: Re: How to use ZLib to zip up a folder in Delphi 2005??? |
|
|
[email]chuayongquan (AT) hotmail (DOT) com[/email] wrote:
| Quote: | If there are some code samples to show me, that would be great!!!
|
Here's a very basic example. It just scans through all the files/folders
(using FindFirst etc) and dumps the data into a stream (pass it a
TCompressionStream if you want it compressed), prepending each file with a
small header giving its name and size. It's not very good in the sense that
you cannot see the directory data without decompressing the whole file.
Changing it to make this possible is left as a simple exercise for the
reader And it doesn't do error checking. And it doesn't store things like
attributes. And ... and ... and ... you get the idea I quickly hacked in
an hour or so and it seems to work, but this is using Delphi 7, so YMMV.
type
TEntryType = (aeFile, aeDirectory, aeEOD);
TArchiveEntry = packed record
case EntryType : TEntryType of
// A file
aeFile:
(
FileNameLen : longint;
FileLength : longint;
);
// A directory
aeDirectory:
(
DirNameLen : longint;
);
// End-of-directory marker
aeEOD:
(
);
end;
procedure CompressDirectory(InDir : string; OutStream : TStream);
var
AE : TArchiveEntry;
procedure RecurseDirectory(ADir : string);
var
sr : TSearchRec;
TmpStream : TStream;
begin
if FindFirst(ADir + '*', faAnyFile, sr) = 0 then
begin
repeat
if (sr.Attr and (faDirectory or faVolumeID)) = 0 then
begin
// We have a file (as opposed to a directory or anything
// else). Write the file entry header.
AE.EntryType := aeFile;
AE.FileNameLen := Length(sr.Name);
AE.FileLength := sr.Size;
OutStream.Write(AE, SizeOf(AE));
OutStream.Write(sr.Name[1], Length(sr.Name));
// Write the file itself
TmpStream := TFileStream.Create(ADir + sr.Name, fmOpenRead or
fmShareDenyWrite);
OutStream.CopyFrom(TmpStream, TmpStream.Size);
TmpStream.Free;
end;
if (sr.Attr and faDirectory) > 0 then
begin
if (sr.Name <> '.') and (sr.Name <> '..') then
begin
// Write the directory entry
AE.EntryType := aeDirectory;
AE.DirNameLen := Length(sr.Name);
OutStream.Write(AE, SizeOf(AE));
OutStream.Write(sr.Name[1], Length(sr.Name));
// Recurse into this directory
RecurseDirectory(IncludeTrailingPathDelimiter(ADir +
sr.Name));
end;
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
// Show that we are done with this directory
AE.EntryType := aeEOD;
OutStream.Write(AE, SizeOf(AE));
end;
begin
RecurseDirectory(IncludeTrailingPathDelimiter(InDir));
end;
procedure DecompressDirectory(InStream : TStream; OutDir : string);
var
AE : TArchiveEntry;
procedure RecurseDirectory(ADir : string);
var
AName : string;
TmpStream : TStream;
begin
ForceDirectories(ADir);
while (true) do
begin
InStream.Read(AE, SizeOf(AE));
case AE.EntryType of
aeFile:
begin
// Recreate the file
SetLength(AName, AE.FileNameLen);
InStream.Read(AName[1], AE.FileNameLen);
TmpStream := TFileStream.Create(ADir + AName, fmCreate or
fmShareExclusive);
TmpStream.CopyFrom(InStream, AE.FileLength);
TmpStream.Free;
end;
aeDirectory:
begin
// Move into the new directory
SetLength(AName, AE.DirNameLen);
InStream.Read(AName[1], AE.FileNameLen);
RecurseDirectory(IncludeTrailingPathDelimiter(ADir +
AName));
end;
aeEOD:
// Done with this directory
break;
else
// Oh dear, corrupted file
end;
end;
end;
begin
RecurseDirectory(IncludeTrailingPathDelimiter(OutDir));
end;
--
Michael Brown
www.emboss.co.nz : OOS/RSI software and more
Add michael@ to emboss.co.nz ---+--- My inbox is always open
|
|
| Back to top |
|
 |
VBDis Guest
|
Posted: Fri Jan 07, 2005 3:32 pm Post subject: Re: How to use ZLib to zip up a folder in Delphi 2005??? |
|
|
Im Artikel <1105072948.938452.155030 (AT) f14g2000cwb (DOT) googlegroups.com>,
[email]chuayongquan (AT) hotmail (DOT) com[/email] schreibt:
| Quote: | If there are some code samples to show me, that would be great!!!
|
Which ZLib implementation do you mean? Supplied with Delphi? With .NET?
DoDi
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|