nsZip - NSP compressor/decompressor to reduce storage

nsZip is an open source PC tool to lossless compress/decompress NSP files in order to save a lot of storage while all NCA files keep their exact same hash. [prebreak]1[/prebreak]
But it’s more than just a compression. A lot of data like NDV0 partitions or fragment NCA3 files will be removed while compressing and exactly recreated when decompressed which saves even more space especially on updates higher than v65536.
In addition, the NSZ format was designed with emulators in mind so adding NSZ support to Yuzu will be possible in the future and because NSZ contains decrypted NCAs no keys would be needed to only extract game files. As compression algorithm Zstandard is used to multithreaded compress 256 KB chunks while uncompressible chunks are stored uncompressed. That way NSPZ/XCIZ allows random read access. Zstandard has a 43MB/s compression and 7032MB/s decompression speed on an 8 threaded CPU at level 18 while having one of the best compression ratios compared to other compression algorithms.

How compressing works: NSP => extracted NSP => decrypted NCAs => trim fragments => compressing to NSZ => verify correctness => repacking NSPZ file
How decompressing works: NSPZ file => extracted NSPZ => decompress NSZ => untrim fragments => encrypt NCAs => verify correctness => repack as NSP

Check out my GitHub page to report bugs, follow this project, post your suggestions or contribute.
GitHub link: https://github.com/nicoboss/nsZip/releases

For the new homebrew compatible NSZ format please see https://github.com/nicoboss/nsz
NSZ thread: https://gbatemp.net/threads/nsz-homebrew-compatible-nsp-xci-compressor-decompressor.550556/
Until NSZ support is implemented in nsZip I don't see why you want to use it instead of NSZ

Block compressed NSZ is very similar to the beautiful NSPZ format just without all the unnecessarily complexity which made NSPZ unfeasible for other software to implement. I might remove NSPZ/XCIZ support in the future if I see that there's no point in keeping it to not confuse people.

Differences between NSZ and NSPZ:

NSPZ/XCIZ:
- GitHub Project: https://github.com/nicoboss/nsZip
- Always uses Block compression allowing random read access to play compressed games in the future
- Decrypts the whole NCA
- Trims NDV0 fragments to their header and reconstructs them
- Only supported by nsZip and unfortunately doesn't really have a future

NSZ/XCZ:
- GitHub Project: https://github.com/nicoboss/nsz
- Uses solid compression by default. Block compression can be enabled using the -B option. Block compression will be the default for XCZ
- Decrypts all sections while keeping the first 0x4000 bytes encrypted. Puts informations needed to encrypt inside the header.
- Deleted NDV0 fragments as they have no use for end users as they only exist to save CDN bandwidth
- Already widely used. Supported by Tinfoil, SX Installer v3.0.0 and probably a lot of other software in the future


nsZip v2.0.0 preview 2:
  • Fixed XCI to XCIZ compression
nsZip v2.0.0 preview 1:
  • New UI made using WPF instead of WinForms
  • Huge performance improvements
    • Directly decrypt the NSP without extracting it first
    • Directly decompressing NSPZ files without extracting them first
    • Huge encrypting speed increase
    • Huge SHA-256 verification speed increase
    • Enhanced compressing multithreading
    • Support for up to 400 CPU Cores
  • Improved game compatibility
  • Command Line support
  • More settings
Version 1.1.0 Changelog:
  • Added support for over multiple NCAs splitted NDV0 fragment trimming
Version 1.0.1 Changelog:
  • Let the user continue when detecting a yet unimplemented multifile NDV0 fragment. I'll add proper support for that unimplemented multifile NDV0 fragment format in the following days.
  • The compression level can now be changed (tradeoff between speed and compression ratio)
  • The block size can now be changed (tradeoff between random access time and compression ratio)
Future planes:
  • NSZ implementation
FAQ:
Q: It prod.keys not found!
A: Dump them using Lockpick and copy them to %userprofile%/.switch/prod.keys
Q: How much storage does this save?
A: It depends on the game. The compressed size is usually around 40-80% of the original size.
Q: What about XCI?
A: Compression to XCIZ is supported, but XCIZ to XCI recreation is still in development.
Q: The program throws an error or seems to behaves not as intended!
A: Open an issue on my GitHub page where you give me exact steps how to reproduce.
Q: Does this compress installed games?
A: Not now but it's planned for the far future.

Screenshot:

nsZip_Screenshot.png
 
Last edited by nicoboss,

TotalJustice

Well-Known Member
Member
Joined
Jan 28, 2017
Messages
443
Trophies
0
Age
26
XP
2,604
Country
United Kingdom
Minor suggestion: Delete the contents of the extract folder after decrypting. Saves space during the process, useful for those with limited space on the c drive.

Extracted 13.5gb => decrypted 13.5gb(27gb total) => compressed 12gb (39gb total)
 
D

Deleted User

Guest
So, basically this program is like the GC shrinker..? does it rip some stuff of the game...?
 

nicoboss

Well-Known Member
OP
Member
Joined
Feb 1, 2019
Messages
132
Trophies
0
Age
26
XP
1,196
Country
Switzerland
LZMA Soon.
The NSZ format supports every compression algorithm you like by its design. LZMA even was registered as 0x02 since its creation. However, ZStandard has a far higher compression/decompression speed and less RAM usage by nearly the same compression ratio and so was chosen as default and the only one supported by nsZip for now. Another very important reason why I've chosen ZStandard over LZMA was its way lower compression ratio decrease when splitted into 256 KB chunks to allow random read access which is required to directly play compressed games in Yuzu/Switch without decompressing the whole game first. This is because unlike LZMA ZStandard doesn’t use a dictionary by default and even if used ZStandard in in dictionary mode one dictionary could be shared across all blocks while LZMA will save it in every block. But in the end adding LZMA support seems not to be that unlikely especially with NSZ type 0 support in mind which won't allow random read access however LZMA definitely doesn't fit to the currently used NSZ type 1 files used to create NSPZ/XCIZ containers and the adding of it would only slow down homebrew integration for absolutely no benefit. In case you're interested here some single threaded compression algorithm compression here you fine one I made for me and the Yuzu team to find the best fitting compression algorithm for NSZ type 1: https://gist.github.com/nicoboss/064e37753de007ea95a29f7fcf8bb3d0 and https://gist.github.com/nicoboss/62ae92c5dd0d44088323dc8b37859abf

It makes it better at at least one thing, so this not out of the question, not it?
Well for now it’s definitely is something PSP can better but also something that will likely be possible in the future for the Switch too. It's kind of sad Nintendo doesn't use NCA instead of something similar to NSZ. It would on average saved Nintendo around 40% bandwidth and storage which would a lot of games allow to be on a smaller less expensive game cards and let the user install more titles on the storage he has and all this for only a very small increase of random-access time. To be fair a lot of other gaming consoles downloads and stored their games uncompressed too and Nintendo is sadly well known for not being that innovative with such things. Generally, compression sadly is extremely underestimated - probably due to the huge internet speed increase and Price/TB drop in the past decades.

Minor suggestion: Delete the contents of the extract folder after decrypting. Saves space during the process, useful for those with limited space on the c drive.

Extracted 13.5gb => decrypted 13.5gb(27gb total) => compressed 12gb (39gb total)
I completely agree. It's just stupid to save every step on HDD instead of always using streams but it really made things way easier to develop and debug. I'll get rid of a lot of this when improving performance and for the next version I will delete temporary data immediately if no longer needed instead of cleaning things up after the task.

So, basically this program is like the GC shrinker..? does it rip some stuff of the game...?
No, this definitely isn't like any shrinker/trimmer. Trimming just removes zero data used to fill the gamecard's physical storage not used by any game data which has nothing to do with compression. nsZip will further reduce the size even if you already trimmed the input files before by around 40% using compression. As a highly oversimplified explanation how compression works: In real world compressing a book would be to write a list with the 999 most used words and replace all of them with that number to save space. In reality file compression is obviously way more complex but basically it tries to reduce redundancy to save as much storage as possible while being able to recreate the exact same file after decompressing. Because normally compressed files don't allow random read access the NCA files are compressed in 256 KB blocks. In addition, only the header of every fragment files is stored as their content can be exactly recreated. In the end you will have the exact same game after decompressing as before compressing as everything is exactly reversable. All NCA files will have the exact same hash then before too and so are exactly the same. As long you let “verify after compress” enabled nsZip makes sure that after decompressing your game will be exactly the same as before compressing and will error if anything went wrong.
 
Last edited by nicoboss,

dAVID_

Well-Known Member
Member
Joined
Oct 23, 2016
Messages
1,405
Trophies
1
Location
The Game
XP
2,276
Country
Mexico
The NSZ format supports every compression algorithm you like by its design. LZMA even was registered as 0x02 since its creation. However, ZStandard has a far higher compression/decompression speed and less RAM usage by nearly the same compression ratio and so was chosen as default and the only one supported by nsZip for now. Another very important reason why I've chosen ZStandard over LZMA was its way lower compression ratio decrease when splitted into 256 KB chunks to allow random read access which is required to directly play compressed games in Yuzu/Switch without decompressing the whole game first. This is because unlike LZMA ZStandard doesn’t use a dictionary by default and even if used ZStandard in in dictionary mode one dictionary could be shared across all blocks while LZMA will save it in every block. But in the end adding LZMA support seems not to be that unlikely especially with NSZ type 0 support in mind which won't allow random read access however LZMA definitely doesn't fit to the currently used NSZ type 1 files used to create NSPZ/XCIZ containers and the adding of it would only slow down homebrew integration for absolutely no benefit. In case you're interested here some single threaded compression algorithm compression here you fine one I made for me and the Yuzu team to find the best fitting compression algorithm for NSZ type 1: https://gist.github.com/nicoboss/064e37753de007ea95a29f7fcf8bb3d0 and https://gist.github.com/nicoboss/62ae92c5dd0d44088323dc8b37859abf


Well for now it’s definitely is something PSP can better but also something that will likely be possible in the future for the Switch too. It's kind of sad Nintendo doesn't use NCA instead of something similar to NSZ. It would on average saved Nintendo around 40% bandwidth and storage which would a lot of games allow to be on a smaller less expensive game cards and let the user install more titles on the storage he has and all this for only a very small increase of random-access time. To be fair a lot of other gaming consoles downloads and stored their games uncompressed too and Nintendo is sadly well known for not being that innovative with such things. Generally, compression sadly is extremely underestimated - probably due to the huge internet speed increase and Price/TB drop in the past decades.


I completely agree. It's just stupid to save every step on HDD instead of always using streams but it really made things way easier to develop and debug. I'll get rid of a lot of this when improving performance and for the next version I will delete temporary data immediately if no longer needed instead of cleaning things up after the task.


No, this definitely isn't like any shrinker/trimmer. Trimming just removes zero data used to fill the gamecard's physical storage not used by any game data which has nothing to do with compression. nsZip will further reduce the size even if you already trimmed the input files before by around 40% using compression. As a highly oversimplified explanation how compression works: In real world compressing a book would be to write a list with the 999 most used words and replace all of them with that number to save space. In reality file compression is obviously way more complex but basically it tries to reduce redundancy to save as much storage as possible while being able to recreate the exact same file after decompressing. Because normally compressed files don't allow random read access the NCA files are compressed in 256 KB blocks. In addition, only the header of every fragment files is stored as their content can be exactly recreated. In the end you will have the exact same game after decompressing as before compressing as everything is exactly reversable. All NCA files will have the exact same hash then before too and so are exactly the same. As long you let “verify after compress” enabled nsZip makes sure that after decompressing your game will be exactly the same as before compressing and will error if anything went wrong.
Is it really that much faster?
68747470733a2f2f6d636d696c6b2e64652f70726f6a656374732f372d5a69702d7a7374642f646c2f636f6d70722d763132302e706e67
 

mrmole

Active Member
Newcomer
Joined
Jul 20, 2018
Messages
32
Trophies
0
Age
34
XP
1,041
Country
United Kingdom
Is it really that much faster?
68747470733a2f2f6d636d696c6b2e64652f70726f6a656374732f372d5a69702d7a7374642f646c2f636f6d70722d763132302e706e67

Please quote your sources, as everybody can make a graph. This particular graph comes from here: https://github.com/mcmilk/7-Zip-zstd and just above it it says: "please note, that the benchmarks are outdated now". Why would you post it, knowing that it is outdated? Simple wikipedia search will tell you that LZMA and and LZMA2 are not exactly the same and there seem to be some arguments that LZMA2 is in fact unsafe for long term archiving: http://lzip.nongnu.org/xz_inadequate.html. Your graph however, does not show the 7zip LZMA2, but Fast LZMA2, which is a 7zip LZMA2, with slightly lower compression ratio that uses parallel buffered radix matchfinder and some optimizations from Zstandard. You should have stated that when presenting the graph, as that is an important information. And lastly the version of ZStandard showed in this graph is from nearly 2 years ago; the newest version is 1.3.8 from about two weeks ago (graph is clearly outdated).

I really know nothing about archiving, but I am all for good research and the transparency of the sources.
 

dAVID_

Well-Known Member
Member
Joined
Oct 23, 2016
Messages
1,405
Trophies
1
Location
The Game
XP
2,276
Country
Mexico
Please quote your sources, as everybody can make a graph. This particular graph comes from here: https://github.com/mcmilk/7-Zip-zstd and just above it it says: "please note, that the benchmarks are outdated now". Why would you post it, knowing that it is outdated? Simple wikipedia search will tell you that LZMA and and LZMA2 are not exactly the same and there seem to be some arguments that LZMA2 is in fact unsafe for long term archiving: http://lzip.nongnu.org/xz_inadequate.html. Your graph however, does not show the 7zip LZMA2, but Fast LZMA2, which is a 7zip LZMA2, with slightly lower compression ratio that uses parallel buffered radix matchfinder and some optimizations from Zstandard. You should have stated that when presenting the graph, as that is an important information. And lastly the version of ZStandard showed in this graph is from nearly 2 years ago; the newest version is 1.3.8 from about two weeks ago (graph is clearly outdated).

I really know nothing about archiving, but I am all for good research and the transparency of the sources.
Sorry, I really did just grab one of the first images.
 

Nazosan

Well-Known Member
Member
Joined
May 12, 2009
Messages
576
Trophies
1
XP
1,089
Country
United States
I really don't understand why CSO keeps getting brought up. It was always a bit tricky. Some things were fine, but I always found more games would run really badly than those which would not. I ended up with just a few CSOs on my PSP versus almost all the rest being just a standard ISO. This should really go without saying -- especially for more modern games -- but doing on-the-fly decompression requires resources. CPU and RAM (but CPU being the biggie I imagine.) And today games for consoles like these are far less optimized than they were in the PSP's era. The percentage of games that would play well with transparent compression is going to be, if anything, even lower. I mean, it will be great if some simpler games do well with this, but overall I really don't think this should be the big push and such a big deal made about it. As for "the PSP could do it so it was better," if you want to be technical, both are computers. They both can do it. Heck, you can use compression on a NES if you really wanted to. The question isn't whether it is physically possible but A. whether it is viable and. B. worth it. Why should battery life be considerably worse, lots of frameskips, etc etc to save a bit here and there? Like I said, in a few games it would be fine I'm sure, but generally speaking the advantage here is not in any potential to run directly on the system itself. Unfortunately, even without any added compression I'm seeing a lot burn through the battery and struggle to maintain a decent framerate as it is because they are just so poorly optimized. One even was probably largely responsible for the stock cooling fan in my system burning out from ramping up to maximum speed continuously for hours at a time (yeah, I learned my lesson from that and use external cooling now.) In the end that's an awful lot of work for probably a lot more problems than the benefits.

Which comes to the real point. The true value of this is absolutely the compression itself being better. Less storage space being used for NSPs is huge. They add up and fill up backup media quite easily -- especially if you have other stuff backed up. (Heck, even just staying on-system, a full NAND backup is quite large, nevermind considering any other things.) It could also definitely help with installation. It's generally easiest by far to put the NSPs on the memory card and install directly. But this comes at the cost of needing double the space free to start. I can't think of a single reason this wouldn't actually be very tinfoil/goldleaf/etc friendly -- all they have to do is integrate block decompression on such a file source and the rest is likely just about the same from there (does it even need to reencrypt? If it did surely even a zero key or something would be sufficient wouldn't it? Nothing complicated needed before the system takes over and does its own thing.) I honestly think that as soon as they become aware of this they will 100% support the idea of it. The size difference on an individual file isn't huge, but it will help -- particularly when one does a bunch at once. Thanks to their encryption it has been impossible to use normal compression techniques, so this is going to help a lot. I think long-term the best thing to do is implement multiple compression techniques and have some sort of mechanism (header or whatever) that specifies which is used (maybe what it already does?) I suspect that the best tradeoff lies in that point where installation doesn't take significantly longer but trading just a bit of install time for overall best compression ratio. I just don't think a "CSO" like thing is all that viable, but if it supports a number of different compression formats and levels the user will be able to try that if they really want to.

nicoboss, I absolutely want to thank you for your work on this. You have a good idea and it's going to help the community.


PS. As for "Nintendo should do something like this to save themselves bandwidth" they still haven't even integrated basic system themes beyond the default not even very good two or given us any form of organizing stuff despite how much of the Switch's OS being based on the 3DS'. They can't even catch up to where they were before. I don't know what the heck is going on there honestly, but clearly optimization and features aren't a focus. I guess they've switched 100% to marketing and game development cycles or something with a bit of anti-piracy measures on the side. Whatever they're doing, it isn't actually working on actually achieving anything useful or nice so much as just maintain status quo. Oh. And "general system stability improvements to enhance the user's experience" of course (I wasn't aware that previous firmwares were so massively unstable until they told me. Phew. Thank goodness for the warning!)
 
  • Like
Reactions: dreambottle

sageharpuiahx

Member
Newcomer
Joined
Nov 14, 2018
Messages
16
Trophies
0
Age
64
XP
88
Country
United States
It struggles to compress anything over a gig, so compressing my triple A titles is out of the question for now, but it's definitely a start
 

nicoboss

Well-Known Member
OP
Member
Joined
Feb 1, 2019
Messages
132
Trophies
0
Age
26
XP
1,196
Country
Switzerland
nsZip version 1.1.0 released: https://github.com/nicoboss/nsZip/releases
I finally implemented over multiple NCA’s splitted NDV0 partitions trimming/untrimming. It took much longer than expected as things were quite complex. This should fix all the update NCAs that throw an error in v1.0.0 and got stored uncompressed after showing a warning message box in v1.0.1. nsZip is written to always throw an error if something went wrong to ensure decompression will give the exact same NCAs as before compressing if verification is on, so please report all errors as I can only test games I own. However, 2 weeks after release without hearing of any other unsupported NSPs it seems as if all are supported by now.
Now that this is done I can finally full focus on your suggestions. In addition, I’ll post a writeup how NDV0 trimming/untrimming works in the next few days.
 

Arnold101

Well-Known Member
Member
Joined
May 19, 2006
Messages
403
Trophies
1
XP
863
Country
United States
the program crashed and left all the temp data on my c drive, now is full, what folder it is? i want to delete temp files.
 

TotalJustice

Well-Known Member
Member
Joined
Jan 28, 2017
Messages
443
Trophies
0
Age
26
XP
2,604
Country
United Kingdom
the program crashed and left all the temp data on my c drive, now is full, what folder it is? i want to delete temp files.
Can’t remember off the top of my head but the folder is called 2.0 which is located in the users/*username*/app folder.

Idk if the latest version changed the folder name so if it’s not there, search for nsZip.exe.

Once you find that folder, you can copy it to another drive with more space and run the exe from there. I had to do that as I didn’t have enough space on the c drive either.
 
Last edited by TotalJustice,

Arnold101

Well-Known Member
Member
Joined
May 19, 2006
Messages
403
Trophies
1
XP
863
Country
United States
Can’t remember off the top of my head but the folder is called 2.0 which is located in the users/*username*/app folder.

Idk if the latest version changed the folder name so if it’s not there, search for nsZip.exe.

Once you find that folder, you can copy it to another drive with more space and run the exe from there. I had to do that as I didn’t have enough space on the c drive either.
not found.
 

TotalJustice

Well-Known Member
Member
Joined
Jan 28, 2017
Messages
443
Trophies
0
Age
26
XP
2,604
Country
United Kingdom
thx, now how i change it? if i can
Change what? Remove the temp files or change its directory?

If it’s temp files, locate the folders compressed, decrypted and extracted (it’s tge folder that has the nszip.exe) delete all files in those folders.

To move it to another drive (like D: drive), just move it and make a shortcut to the nszip.exe.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • Julie_Pilgrim @ Julie_Pilgrim:
    the internet
  • Julie_Pilgrim @ Julie_Pilgrim:
    @Psionic Roshambo i have 16 gb in my pc and i run into issues with ram more than i'd like to admit
  • HiradeGirl @ HiradeGirl:
    I got only 8GB of RAM. But I want 32GB.
  • Sonic Angel Knight @ Sonic Angel Knight:
    Time to just download more ram
  • K3Nv2 @ K3Nv2:
    Yeah search Google
  • Sonic Angel Knight @ Sonic Angel Knight:
    Or, I also heard that if you use flash memory, it can act as more "RAM" at least windows tell me when I stick a flash drive into it.
  • Veho @ Veho:
    It can act as a swap drive but that isn't more RAM, it's slooow.
  • K3Nv2 @ K3Nv2:
    I wish we could have 1Gbps external storage by now
  • K3Nv2 @ K3Nv2:
    Like for micro
  • Veho @ Veho:
    New Myoo.
  • SylverReZ @ SylverReZ:
    @Veho, Yooo noice
  • SylverReZ @ SylverReZ:
    Looks like a Famicom handheld
  • Veho @ Veho:
    Yeah, they were going for that.
  • Veho @ Veho:
    It's not very good though.
  • Veho @ Veho:
    I'm watching the review, the emulators it uses suck bawls.
  • Veho @ Veho:
    Software update might improve it.
  • Psionic Roshambo @ Psionic Roshambo:
    Or maybe someone will make like Emulation Station for it or something?
  • Veho @ Veho:
    That counts as a software update :tpi:
    +1
  • OctoAori20 @ OctoAori20:
    Ello
  • K3Nv2 @ K3Nv2:
    I can think of the design teams process another joystick and no audio or a joystick and mono audio
  • Veho @ Veho:
    "You think we can just put the speakers at the top
    ?" "NO!"
    +1
  • K3Nv2 @ K3Nv2:
    Pft stereo speakers you're fired
    +1
    K3Nv2 @ K3Nv2: Pft stereo speakers you're fired +1