Author Topic: String Compression and Decompression  (Read 101 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2340
    • ScriptBasic Open Source Project
String Compression and Decompression
« on: December 17, 2018, 05:37:50 PM »
Peter on the BaCon forum is experimenting with string compression.

compression and decompression

Here is the Script BASIC solution.

Code: Script BASIC
  1. IMPORT t.bas
  2. IMPORT zlib.bas
  3.  
  4. PRINT FORMAT("War & Peace uncompressed is %~#,###,###~ bytes. Compressed it's %~#,###,###~ bytes.\n", _
  5.       FILELEN("warpeace.txt"), LEN(zlib::Compress(t::LoadString("warpeace.txt"))))
  6.  


$ time scriba wpc.sb
War & Peace uncompressed is 3,202,941 bytes. Compressed it's 1,195,214 bytes.

real   0m0.385s
user   0m0.377s
sys   0m0.008s
$

« Last Edit: December 17, 2018, 08:38:31 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2340
    • ScriptBasic Open Source Project
Re: String Compression and Decompression
« Reply #1 on: December 17, 2018, 09:12:06 PM »
Here is a file compress and uncompress example.

The file being compressed is deleted with the gzip() function, You may want to make a backup before playing with this.

You can use standard Linux utilities to uncompress files compressed with the zlib extension module.

Code: Script BASIC
  1. IMPORT zlib.bas
  2.  
  3. PRINT FILELEN("warpeace.txt"),"\n"
  4. zlib::gzip "warpeace.txt", "warpeace.gz"
  5. PRINT FILELEN("warpeace.gz"),"\n"
  6. zlib::gunzip("warpeace.gz", "warpeace.txt")
  7. PRINT FILELEN("warpeace.txt"),"\n"
  8.  


$ time scriba unwpc.sb
3202941
1195226
3202941

real   0m0.450s
user   0m0.433s
sys   0m0.016s
$

« Last Edit: December 19, 2018, 01:58:55 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 504
Re: String Compression and Decompression
« Reply #2 on: December 18, 2018, 10:00:39 PM »
Using MBC and libzip(uses zlib):

Code: Text
  1. $MODULE ZIP.mbc
  2. $EXECON
  3.  
  4.  
  5. dim fname$,ret
  6.  
  7. if G_argc < 2 then
  8.     print "usage: ",command$(0)," archive"
  9.     end = 1
  10. end if
  11.  
  12. fname$ = command$(1)
  13.  
  14. print E"Source file size:\t";lof%(fname$)/1000;"kB"
  15.  
  16. ret = Compress(fname$,"W&P.zip")
  17. if ret then
  18.     print E"Compressed file size:\t",ret/1000,"kB"
  19. else
  20.     end = 1
  21. end if
  22.  
  23. ret = Extract("W&P.zip","UNPACKED-W&P.txt")
  24. if ret then
  25.     print E"Extracted  file size:\t",ret/1000,"kB"
  26. end if

Code: [Select]
[riveraa@MacDev ~/Projects/mbc/compression] $ time ./ziptest "war and peace.txt"
Source file size: 3359kB
Compressed file size: 1220kB
Extracted  file size: 3359kB

real 0m0.417s
user 0m0.404s
sys 0m0.009s

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 2340
    • ScriptBasic Open Source Project
Re: String Compression and Decompression
« Reply #3 on: December 18, 2018, 11:32:15 PM »
MBC sure looks easier to use than BaCon with its C helper dependencies.