OpenFile() returns 0 after using OpenPack()

Just starting out? Need help? Post your questions and find answers here.
ZX80
Enthusiast
Enthusiast
Posts: 330
Joined: Mon Dec 12, 2016 1:37 pm

OpenFile() returns 0 after using OpenPack()

Post by ZX80 »

Hi to all.

First I open incomplete archive to read header and get information about file size, inside archive. Then close zip-file. Now you need open archive again to append data. But result of OpenFile()-function is always zero value. Why? I closed the archive before. What's wrong?

Code: Select all

UseZipPacker()

FLink$ = "c:\tmp\1.zip"

UnPackedSize.l = 0
File = OpenPack(#PB_Any, FLink$)
If File
  If ExaminePack(File)
    While NextPackEntry(File)
      UnPackedSize = PackEntrySize(File, #PB_Packer_UncompressedSize)
    Wend
  EndIf
  ClosePack(File)
Else
  MessageRequester("Error", "Сan't open archive.")
  End
EndIf
  
Debug "UnPackedSize: " + Str(UnPackedSize)
If UnPackedSize = 0
  MessageRequester("Error", "Archive is corrupted.")
  End
EndIf


File = OpenFile(#PB_Any, FLink$, #PB_File_Append)
Debug "Open File: " + Str(File)
If File
  CloseFile(File)
EndIf
P.S. In my case archive contains only one file.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: OpenFile() returns 0 after using OpenPack()

Post by Marc56us »

Now you need open archive again to append data

Code: Select all

OpenFile(#PB_Any, FLink$, #PB_File_Append)
To use archive, you need to OpenPack, not OpenFile, but as far as I know, you can't add file to an existing archive with the PB lib.

Have a look at: ZIP module - remove and add files from/to existing archives
https://www.purebasic.fr/english/viewto ... 12&t=66053
(there may be more recent)
ZX80
Enthusiast
Enthusiast
Posts: 330
Joined: Mon Dec 12, 2016 1:37 pm

Re: OpenFile() returns 0 after using OpenPack()

Post by ZX80 »

Marc56us, thanks for your reply.

My question is related to the previous topic. Look the code above carefully, please.
To use in archive, you need to OpenPack, not OpenFile, but as far as I know, you can't add data to an existing archive with the PB lib.
Yes, you are right. But I don’t need it. I'm not looking for this and I didn’t ask that. I used the OpenPack()-function only to get unpacked file size. That's all. Next time I work with archive as with a regular file. Why not? After all, this is not prohibited.

P.S. Please forgive me for my bad English. Perhaps this is reason that you did not understand me.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: OpenFile() returns 0 after using OpenPack()

Post by Marc56us »

All right, that said with your code, I don't get an error message when opening an archive file with OpenFile()
(I created the .zip, containing a single file with TC)
OpenFile() does return a number other than zero, so open OK

Is it a corrupted or empty archive?

I have "Archive is corrupted." if zip archive is empty and program exit because of "End".
If I remove "End", programme continue and OpenFile is OK (empty zip file is 22 bytes)

PB 5.72 LTS x64 Windows 10 x64

I create a test archive at each startup to make sure that no other program keeps a handle on the archive.

Code: Select all

UseZipPacker()

; Create new test file to be sure no file handle left
FLink$ = GetTemporaryDirectory() + "Test_" + FormatDate("%hh%ii%ss", Date()) + ".zip"
Debug "File Test: " + FLink$
CreatePack(0, FLink$)
AddPackFile(0, GetTemporaryDirectory() + "PB_EditorOutput.pb", "test")
ClosePack(0)


UnPackedSize.l = 0
File = OpenPack(#PB_Any, FLink$)
If File
  If ExaminePack(File)
    While NextPackEntry(File)
      UnPackedSize = PackEntrySize(File, #PB_Packer_UncompressedSize)
    Wend
  EndIf
  ClosePack(File)
Else
  MessageRequester("Error", "Сan't open archive.")
  End
EndIf
 
Debug "UnPackedSize: " + Str(UnPackedSize)
If UnPackedSize = 0
  MessageRequester("Error", "Archive is corrupted.")
  End
EndIf


File = OpenFile(#PB_Any, FLink$, #PB_File_Append)
Debug "Open File: " + Str(File)
If File
  CloseFile(File)
EndIf
I hope this helps a little. Having said that, you'r right, English is not my native language either, so maybe I didn't understand the problem at all. I'm sorry. :wink:
ZX80
Enthusiast
Enthusiast
Posts: 330
Joined: Mon Dec 12, 2016 1:37 pm

Re: OpenFile() returns 0 after using OpenPack()

Post by ZX80 »

I need UnPackedSize value, so that after complete file downloading to perform additional verification (compare two values). UnPackedSize value on the server and real file size after unpacking to know that
file didn't break on the road).
Is it a corrupted or empty archive?
Neither one. This is not a fully downloaded archive. However, it has correct header. As well as a chunk of data correct relative to the original file.

Please, launch this code, which kindly provided by infratec:

Code: Select all

#BlockSize = 1024000

InitNetwork()

;Debug ReceiveHTTPFile("http://download.2gis.com/arhives/2GISData_Minvody-84.0.0.zip", "c:\tmp\2GISData_Minvody-84.0.0.zip")

NewMap HeaderMap$()

File = CreateFile(#PB_Any, "c:\tmp\2GISData_Minvody-84.0.0.zip")
If File
 
  Repeat
    ResetMap(HeaderMap$())
    Debug "bytes=" + Str(StartAddr) + "-" + Str(StartAddr + #BlockSize - 1)
    HeaderMap$("Range") = "bytes=" + Str(StartAddr) + "-" + Str(StartAddr + #BlockSize - 1)
    HTTPRequest = HTTPRequest(#PB_HTTP_Get, "http://download.2gis.com/arhives/2GISData_Minvody-84.0.0.zip", "", 0, HeaderMap$())
    If HTTPRequest
      *Buffer = HTTPMemory(HTTPRequest)
      If *Buffer
        WriteData(File, *Buffer, MemorySize(*Buffer))
        If MemorySize(*Buffer) < #BlockSize
          Exit = #True
        EndIf
        FreeMemory(*Buffer)
      EndIf
      FinishHTTP(HTTPRequest)
    EndIf
    StartAddr + #BlockSize
  Until Exit
 
  CloseFile(File)
EndIf
Wait a while. Then stop processing. Now you have source data. That is, only part of the full archive. Now run the code above(from my first post). FLink$ = path to partially downloaded file.

PB 5.72 LTS & Windows 7. Both are x86.


P.S. Marc56us, thank you for try to help me. Your code is OK. Now try what I do, please.
P.S.2 I apologize, but I have to go. I’ll read your answers only tomorrow. Thanks in advance for your help.
ZX80
Enthusiast
Enthusiast
Posts: 330
Joined: Mon Dec 12, 2016 1:37 pm

Re: OpenFile() returns 0 after using OpenPack()

Post by ZX80 »

This check is also necessary to know, that this is the same file, and not another.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenFile() returns 0 after using OpenPack()

Post by infratec »

Hmm....

this works:

Code: Select all

OpenFile(#PB_Any, FLink$, #PB_File_Append|#PB_File_SharedWrite)
I think it is a bug.

If I look at a test programm via SysInternals ProcMon, I see that the file is not closed after ClosePack().
ZX80
Enthusiast
Enthusiast
Posts: 330
Joined: Mon Dec 12, 2016 1:37 pm

Re: OpenFile() returns 0 after using OpenPack()

Post by ZX80 »

infratec, thank you very much for your attention to the problem.

Marc56us, thank you too for your replies.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenFile() returns 0 after using OpenPack()

Post by infratec »

ZX80
Enthusiast
Enthusiast
Posts: 330
Joined: Mon Dec 12, 2016 1:37 pm

Re: OpenFile() returns 0 after using OpenPack()

Post by ZX80 »

infratec, thanks for all.
"As you can see when the NextPackEntry() loop is in use,
the file test.zip is not closed before an access to test.123"
You are absolutely right. Good catch.
If I wrote code like this:

Code: Select all

...
File = OpenPack(#PB_Any, FLink$)
If File
  If ExaminePack(File)
;   While NextPackEntry(File)
    NextPackEntry(File)
    UnPackedSize = PackEntrySize(File, #PB_Packer_UncompressedSize)
;   Wend
  EndIf
  ClosePack(File)
Else
...
then everything works(without #PB_File_SharedWrite flag). The archive consist of only one file. It’s known in advance. Therefore, the cycle(file enumeration inside archive) is not needed.

Fred, thanks a lot. We will wait.

P.S. I'm glad that I brought a little benefit. Unaware of it (inadvertently).
Post Reply