BASIC User Group > Code Challenges

String Compression and Decompression

(1/2) > >>

Peter on the BaCon forum is experimenting with string compression.

compression and decompression

Here is the Script BASIC solution.

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

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

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

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 ---IMPORT zlib.bas PRINT FILELEN("warpeace.txt"),"\n"zlib::gzip "warpeace.txt", "warpeace.gz"PRINT FILELEN("warpeace.gz"),"\n"zlib::gunzip("warpeace.gz", "warpeace.txt")PRINT FILELEN("warpeace.txt"),"\n" 

$ time scriba

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

Using MBC and libzip(uses zlib):

--- Code: Text ---$MODULE ZIP.mbc$EXECON  dim fname$,ret if G_argc < 2 then    print "usage: ",command$(0)," archive"    end = 1end if fname$ = command$(1) print E"Source file size:\t";lof%(fname$)/1000;"kB" ret = Compress(fname$,"W&")if ret then    print E"Compressed file size:\t",ret/1000,"kB"else    end = 1end if ret = Extract("W&","UNPACKED-W&P.txt")if ret then    print E"Extracted  file size:\t",ret/1000,"kB"end if

--- Code: ---[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
--- End code ---


MBC sure looks easier to use than BaCon with its C helper dependencies.


--- Code: Go ---package main import (        "fmt"        "log"        "os"         ""        "") func main() {        // List of Files to Zip        files := []string{"warpeace.txt"}        output := ""        output2 := "warpeace.txt"         if err := archiver.Archive(files, output); err != nil {                log.Fatal(err)        }         if err := archiver.Unarchive(output, "test"); err != nil {                log.Fatal(err)        }        fmt.Println("Source File Size:", humanize.Bytes(getFileSize("warpeace.txt")))        fmt.Println("Compressed File Size:", humanize.Bytes(getFileSize(output)))        fmt.Println("Extracted File Size:", humanize.Bytes(getFileSize("test/"+output2))) } func getFileSize(filename string) uint64 {        file, err := os.Open(filename)        if err != nil {                // handle the error here                return 0        }        defer file.Close()         // get the file size        stat, err := file.Stat()        if err != nil {                return 0        }        return uint64(stat.Size())} 

Source File Size: 3.2 MB
Compressed File Size: 1.2 MB
Extracted File Size: 3.2 MB

real   0m0.248s
user   0m0.224s
sys   0m0.019s



[0] Message Index

[#] Next page

Go to full version