Re: Reading Zip file contents without DLL's... How?

From: Maarten Wiltink (maarten_at_kittensandcats.net)
Date: 01/03/04


Date: Sat, 3 Jan 2004 22:15:03 +0100


"Link" <mickeym@NOSPAM.go.to> wrote in message
news:3ff60c51$0$69960$edfadb0f@dread12.news.tele.dk...

>>> FileSeek(ZipFile,26,0);
>>
>> Zero. How illuminating.
>
> If we can trust the documentation, this searches to byte 26 from the
> beginning of the file. The syntax is FileSeek(Handle, Offset, Origin),
> where a value of 0 for Origin means from the beginning of the file.

Yes, that's what it says. That doesn't make me any happier about it.
There should be some constants defined somewhere, and documented with
FileSeek.

[...]
>>> FileRead(ZipFile,ExtSizePointer^,2);
>>> FileSeek(ZipFile,30,0);
>>> NameSize:=integer(ExtSizePointer);
>>
>> No, ExtNamePointer^.
>
> Why? The data comes from the above FileRead, which SHOULD assign a
> value of 50 to ExtSizePointer. And ExtNamePointer isn't allocated
> memory until this next line:

Curses. My mistake. I meant "No, ExtSizePointer^". Dereference the
pointer, do not cast it to an integer.

[...]
> The difference between this and the sample code this was based from,
> is that I use a PString, rather than a PChar. Does that really make
> any difference? Isn't a PChar just a PString that can only contain
> 1 character?

Pointers do not contain things; they point to them. (Actually, of course,
they contain memory addresses.)

A PChar points to a single character, but through pointer arithmetic you
can change what character it points to. Adding one to a PChar value will
give you a new pointer that points to the _next_ character. Please think
about this; it's quite fundamental and important. Adding one to a PChar
variable will make the variable point to the next character (- change the
value in the variable to one that points to the next character).

A PString value points to a (long) string value, which in Delphi is a
pointer itself and is not yours to manage. String variables may be used
_as strings_ in your code and the Delphi run-time system will manage the
memory containing the strings, and the pointer in the variable that points
to it, for you. At this point, you should remember two things: that strings
are _strings_ at the language level, and that pointers to strings are not
useful for your current purpose.

[...]
>> Assuming that there's a 4-byte length at offset 26, and a string buffer
>> of the indicated length at offset 30, this code might work (untested):
>>
>> function FirstFilenameInZipfile(const ArchiveFilename: String): String;
>> var
>> Archive: File;
>> Length: Integer;
>> begin
>> Assign(Archive, ArchiveFilename); Reset(Archive);
>> try
>> Seek(Archive, 26);
>> BlockRead(Archive, Length, SizeOf(Length));
>> SetLength(Result, Length);
>> BlockRead(Archive, Result[1], Length);
>> finally
>> Close(Archive);
>> end;
>> end;

> After a few modifications to adapt it to my need (changing Length to
> a Word, adding a seek to skip the 2 bytes between the things to read,
> reading the filename to a separate string, and processing that string),
> it still doesn't work, setting Length to a value of 63975 (should be 50).

More curses. Error checking would have been nice at this point; the
problem is in the default record size assumed by Reset and proper testing
would have revealed that no data were in fact read.

Amend code to "Reset(Archive, 1);".

> Also, I can't seem to find any help entry for your usage of Seek.

Type "seek", press F1, read help.

Groetjes,
Maarten Wiltink



Relevant Pages

  • Re: GetOpenFilename()
    ... Specifies that the File Name list box allows multiple selections. ... the directory and file name strings are NULL ... You advance the pointer one character more. ...
    (microsoft.public.vc.language)
  • Re: MFC Interview Tests
    ... Note that in the days of this example, we were still using only 8-bit character strings. ... Surrogate pairs pose a huge number of problems, and by the time we start seriously moving ... pointer pointing to the end of the string. ...
    (microsoft.public.vc.mfc)
  • Re: Search for a string backwards in a file.
    ... default fstreams already read the file into blocks of strings (though the ... when the user reads the last character, ... streambuf's get pointer to the last character, ... Call file.rdbufto get a pointer to the streambuf. ...
    (comp.lang.cpp)
  • Re: Char Array, PChar, and String
    ... appears that a zero based character array can be used as a PChar or String, while the one based array can only be used as a PChar. ... My interest in this is based on an application I am working on which reads a binary file which contains multiples of 16 bytes that may have one or more null terminated character strings. ...
    (comp.lang.pascal.delphi.misc)
  • Re: When will delphi have a unicode index operator ? ;)
    ... > operator would always returns a full character. ... PChar and return a new PChar for the beginning of the next character in ... > strings as well? ... You should use the Ansi versions of the functions when possible. ...
    (alt.comp.lang.borland-delphi)