Unpacking .7z *with* subdirectories - BUG in NextPackEntry()

Just starting out? Need help? Post your questions and find answers here.
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Unpacking .7z *with* subdirectories

Post by Bisonte »

In my case I use this routine with ZIP but it should work with 7z...

Code: Select all

Procedure.i CreateDirectoryEx(DirectoryName.s, FileAttribute = #PB_Default)
  
  Protected i, c, tmp.s
  
  If Right(DirectoryName, 1) = #PS$
    DirectoryName = Left(DirectoryName, Len(DirectoryName) -1)
  EndIf
  c = CountString(DirectoryName, #PS$) + 1
  For i = 1 To c
    tmp + StringField(DirectoryName, i, #PS$)
    If FileSize(tmp) <> -2
      CreateDirectory(tmp)
    EndIf
    tmp + #PS$
  Next
  
  If FileAttribute <> #PB_Default
    SetFileAttributes_(DirectoryName, FileAttribute)
  EndIf
  
  If FileSize(DirectoryName) = -2
    ProcedureReturn #True
  EndIf
  
EndProcedure

UseLZMAPacker()

Define ID, DestinationPath.s, Archive.s, File.s

Archive = "pathandfile.7z"
DestinationPath = "PathTo"

ID = OpenPack(#PB_Any, Archive, #PB_PackerPlugin_Lzma)

If ID
  If ExaminePack(ID)
    While NextPackEntry(ID)
      File = DestinationPath + PackEntryName(ID)
      CreateDirectoryEx(GetPathPart(File))
      UncompressPackFile(ID, File)
    Wend
  EndIf
  ClosePack(ID)
EndIf
  
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Unpacking .7z *with* subdirectories

Post by Marc56us »

The is a new version of 7-zip (v22.00) since 2022-06-15 with bugfix.
https://www.7-zip.org/history.txt
Maybe update in the next beta? (PB6B10 is 2022/06/09)
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Unpacking .7z *with* subdirectories

Post by StarWarsFan »

NextPackEntry(ID) is the problem. It gives NULL = FALSE in the described circumstances
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Unpacking .7z *with* subdirectories

Post by StarWarsFan »

@Bisonte: Nice code, thank you.

BUT: Same issue there.

This happens when the archive includes at least one empty subdirectory:
Both OpenPack() and ExaminePack() return #TRUE, so up to there it is good, but: NextPackEntry() returns 0!
Result: So the loop is not started at all. Nothing *at all* is unpacked.

So the more I look at this I clearly see this is not an issue of 7zip.
There is a problem with how PureBasic's NextPackEntry() handles the contents of the archive.
There are apparently yet more circumstances where NextPackEntry() returns 0.
(yet to be investigated)
I have just found another 7z archive (without any subdirs) that cannot be unpacked either.

How to reproduce this bug:

STEP 1
Create a new 7z archive and put 1 textfile with some short text in
Result: Unpacks fine.

STEP 2
Now create an empty subdirectory and add that to the 7z-archive that you have just successfully unpacked.
Result: Nothing is unpacked suddenly. Nothing AT ALL, so not even the file that was just successfully unpacked in step 1
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Unpacking .7z *with* subdirectories

Post by BarryG »

(Post and code deleted because it doesn't work with ChrisR's "test.7z" archive below).
Last edited by BarryG on Sun Jun 26, 2022 11:32 am, edited 2 times in total.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Unpacking .7z *with* subdirectories

Post by Marc56us »

With me, full or empty folder, created before or after; it works without problem.
(Windows 10 x64, 7Zip 22.00 x64 (or via Total Commander 10.00))

It may (maybe) depend on how you created the archive? Normal or solid archive?
A solid archive is not organized in the same way as soon as you modify it. It may not be read in the same way.
"New versions of 7-Zip (starting from version 15.06) use another file sorting order by default for solid 7z archives."
https://www.7-zip.org/faq.html
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Unpacking .7z *with* subdirectories

Post by ChrisR »

I did not reproduce with the 2 steps written above
But I've reproduced the error on NextPackEntry() with just 2 files, one Ansi and the other Utf8 with Bom.

I put the archive here for testing: test.7z (1ko)

Tested with Marc's code:

Code: Select all

; Uncompress archive With sud dir creation
; Marc56us 2022-01-18
File7z.s = "E:\Temp\test\test.7z"
SetCurrentDirectory(GetPathPart(File7z))
UseLZMAPacker()
If OpenPack(0,file7z) 
  If ExaminePack(0)
    While NextPackEntry(0) 
      Debug PackEntryName(0)
      If PackEntryType(0) = #PB_Packer_Directory
        CreateDirectory(PackEntryName(0))
      Else
        UncompressPackFile(0, "" + PackEntryName(0), PackEntryName(0))
      EndIf
    Wend
  EndIf
EndIf
ClosePack(0)
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Unpacking and packing .7Z ---> see newest message

Post by StarWarsFan »

Many thanks again, Marc, you yourself have posted the following on page one of this topic, I quote because I find this to be true:
Marc56us wrote: Tue Jan 18, 2022 4:48 pm Edit: Yes, this works with many archives (Almost all the ones I tried). I'm trying to identify which features have the ones that don't work.
All the ones I created (zip or 7z) with TC or 7zip and most of the others also decompress without problem with this simple code.
I will look for the difference with the others.
So there are archives that can be unpacked using what we are discussing here and there are obviously archives that cannot be unpacked this way.
The discussion how I create the archive is irrelevant, that would be leading into the wrong direction, because I can not know nor influence on how OTHER users create their archive ---> The aim of this discussion is solely to find a way to unpack ANY 7z-archive, no matter how it is created or where it comes from.

Obviously some archives collide with the way NextPackEntry() is conducted. => on some archives it returns #false (0) here and the loop is not executed. What causes this I would like to understand so I can find a way that works with ANY 7z archive

Hope I am clearer now. Apologies, English is not my first language, I am really giving my best, guys!
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
tester
User
User
Posts: 30
Joined: Sun Dec 28, 2014 1:12 pm

Re: Unpacking .7z *with* subdirectories

Post by tester »

StarWarsFan wrote: Sat Jun 25, 2022 11:45 am...Obviously some archives collide with the way NextPackEntry() is conducted. => on some archives it returns #false (0) here and the loop is not executed...
Can you provide a sample of such an "incorrect" archive?
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Unpacking .7z *with* subdirectories

Post by ChrisR »

tester wrote: Sat Jun 25, 2022 4:57 pm Can you provide a sample of such an "incorrect" archive?
I uploaded an example 3 post above.
Not an incorrect 7z archive, but an example to reproduce the bug on NextPackEntry()
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Unpacking .7z *with* subdirectories

Post by StarWarsFan »

Right, Chris.

I also ask myself if this is a bug in PureBasic v5.71 (the version I am still using)
Chris, which version have you used to reproduce that bug?
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
Jeromyal
Enthusiast
Enthusiast
Posts: 204
Joined: Wed Jul 17, 2013 8:49 am

Re: Unpacking .7z *with* subdirectories

Post by Jeromyal »

I have noticed that files without an extension causes the loop to fail as well.

So empty folders, and files with no extensions, as well as files with zero size?
Am I right?
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Unpacking .7z *with* subdirectories

Post by BarryG »

@StarWarsFan: What about using Kenmo's 7zip module? -> https://github.com/kenmo-pb/includes
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Unpacking .7z *with* subdirectories

Post by StarWarsFan »

@Barry: Nothing against that. Did not know it existed. But I thought we would have a short and working version after I created this thread and everybody came and said it works.
Jeromyal wrote: Sat Jul 09, 2022 7:47 am I have noticed that files without an extension causes the loop to fail as well.

So empty folders, and files with no extensions, as well as files with zero size?
Am I right?
Yes, Jeromy, looks like it.

And definitely: It is not the issue how you create that archive or any like that. It is a bug in NextPackEntry() that creates this problem we discuss here. NextPackEntry() simply does not handle the above listed situations right and exits with #FALSE as if there were no entries left at all to be worked on.

Is this reported as a bug yet?
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
puchna8
New User
New User
Posts: 1
Joined: Mon Jul 18, 2022 6:04 pm

Re: Unpacking .7z *with* subdirectories - BUG in NextPackEntry()

Post by puchna8 »

Post Reply