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

Just starting out? Need help? Post your questions and find answers here.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

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

Post by StarWarsFan »

Hello friends,

I have read somewhere in the German area of this forum, that PB has native support for UNPACKING .7z files from V5.10
Unfortunately in that thread all links are dead, that is why I could not download it.

In the documentation for "OpenPack()" I was neither able to locate support for .7z
Other threads mention that a Use7zip() command would come (or similar), but I can not find acccurate any current info

Can somebody please shed some CURRENT news-light on this? Much appreciated!
Last edited by StarWarsFan on Sat Jul 16, 2022 6:28 am, edited 7 times in total.
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.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Unpacking and packing .7Z

Post by helpy »

Have a look at PureBasic help:
History in PureBasic help wrote:16th October 2015 : Version 5.40 LTS
- Added: 7z archive creation
- Removed: #PB_Packer_CompressedSize support for ZIP and 7z archives with PackerEntrySize()

14th February 2013 : Version 5.10
- Added: Fully reworked Packer library with ZIP, BriefLZ, 7z (unpacker only), LZMA and JCALG1 (Windows x86 and UncompressMemory() only) support. JCALG1 support has been dropped. Archives formats for BriefLZ have been changed.
PureBasic help: CreatePack() wrote:Parameter "Plugin" for CreatePack() command:

#PB_PackerPlugin_Lzma : use the Lzma packer to create the pack file (also known as 7z archive). UseLZMAPacker() has to be called to register the plugin.
PureBasic help: UseLZMAPacker() wrote:Syntax
    UseLZMAPacker()

Description
Enable LZMA compress, uncompress and 7z archive support to the packer library. LZMA compression is considered as one of the best available multipurpose compression algorithm. It provides very good compress ratio and fast uncompress. Compressing can be slow.
I think "UseLZMAPacker()" is your friend ;-)
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Unpacking and packing .7Z

Post by StarWarsFan »

Ah! Nice!

Many thanks to you!
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 and packing .7Z

Post by StarWarsFan »

Can somebody from the admins please mark this thread [SOLVED] ?
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.
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: Unpacking and packing .7Z

Post by Derren »

You can edit your first post's title on your own.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Unpacking and packing .7Z

Post by StarWarsFan »

Ah! Okay, done!

And thanks!
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 and packing .7Z

Post by StarWarsFan »

Okay, I got one more question. Unpacking works so far, beside subdirectories, those are not created, those are apparently ignored.

What am I doing wrong?

My short code:

Code: Select all

UseLZMAPacker()
If OpenPack(0,"file.7z") And ExaminePack(0)
  While NextPackEntry(0) : 
    UncompressPackFile(0,path$+ReplaceString(PackEntryName(0),"/","\"))
  Wend
EndIf
ClosePack(0)
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.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

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

Post by Marc56us »

Unpacking works so far, beside subdirectories, those are not created, those are apparently ignored.
Yes and No,
UncompressPackFile() can use sub directory if exists, but can't create it if not exists.
So you need to create a recurisve function for create each directory. (split full name in archive)
Use something like If PackEntryType(hPack) = #PB_Packer_Directory ... CreateDirectory ...
There are samples in this forum. I don't know what is the best code.
:wink:
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

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

Post by Marc56us »

To my great surprise, this seems to work (?)
(Without recursive function). The directories are created as you go along.

Code: Select all

; Uncompress archive With sud dir creation
; Marc56us 2022-01-18
UseLZMAPacker()
If OpenPack(0,"file.7z") And 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
ClosePack(0)
This may be a special case of some archives ?
It's too simple, I must have forgotten something ?
Tested with a zip archive and a 7z archive (replacing only the Use... call)

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.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

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

Post by StarWarsFan »

Ha! My code looks almost exactly the same and as long as I unpack to the current working directory things seem to work:

Code: Select all

UseLZMAPacker()
OpenPack(0,"file.7z") : 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
ClosePack(0)
But as soon as I want to unpack to a different destination path, things get out of order...
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.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

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

Post by Marc56us »

StarWarsFan wrote: Tue Jan 18, 2022 10:38 pm Ha! My code looks almost exactly the same and as long as I unpack to the current working directory things seem to work:
...
But as soon as I want to unpack to a different destination path, things get out of order...
Hi StarWarsFan,

Yes, same for me and I don't understand too why the destination doesn't support a full path.
In the meantime I solved by putting a temporary SetCurrendDirectoy() to the destination folder.
I'm still looking for that and still why some archives don't work.

:wink:
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Unpacking .7z *with* subdirectories

Post by StarWarsFan »

No toy, it's a directory :)

SetCurrentDirectory()
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 »

Returning to this, bringing some update.

I seem to have found why some 7z-archives work and some do not.
What I have encountered is that if a 7z-archive has got a subdirectory as its VERY FIRST ENTRY,
it will not work, that entry is NOT recognized by "While NextPackEntry(0)" and consequently the loop is not even executed.

The code that I am using right now is:

Code: Select all

UseLZMAPacker()
If OpenPack(0,"pathandfile.7z") And 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
ClosePack(0)
Any ideas to circumvent this problem?
Is there a fix that I have probably overlooked?
Or what am I doing wrong?
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 »

Confused.

The more I look at this the less I understand. I have now run two tests.
1)
I created a fresh test archive with some files but NO subdirectories.
Result: The above routine (I use Marc's as mentioned earlier) unpacks the files. No errors

2)
I now took THAT archive and added two subdirectories to that archive.
one of them is empty, one of them has files in it.
Result: Nothing at all is unpacked. NextPackEntry(0) returns 0, so the loop is not started at all.

When I remove those two subfolders from that archive it works again as explained in (1)

How can I get this done? Anybody? Any help?
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 »

Really weird!
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.
Post Reply