Newlisp / Zlib

Started by lispen, April 11, 2006, 03:37:13 PM

Previous topic - Next topic

lispen

Hello,



  I'm trying to use Zlib from Newlisp using "Import" to compress a jpeg image from a file. Has anyone does this?  What I'd like to do is compress the jpeg file, it would then be send compressed to my server and uncompress it via PHP.  



   Any help would be appreciated.



http://www.zlib.net/">http://www.zlib.net/



Thanks,

Ken

HPW

#1
Hello lispen,



Isn't jpg compressed enough?



Anyway, you may have a look at:



http://hpwsoft.de/anmeldung/html1/newLISP/newLISP2.html">http://hpwsoft.de/anmeldung/html1/newLISP/newLISP2.html



(import "hpwNLUtility.dll" "hpwZlibCompressFile")

(get-string(hpwZlibCompressFile "mySource.ext" "myTarget.ext"))
Hans-Peter

Lutz

#2
This code works for me on Mac OS X and should work on Win32 and Linux/UNIX too. Just change "libz.dylib" to "libz1.dll" or whatever the name is on Win32



; - zlib.lsp
;
; functions for cpmpression/decompression with zlib from http://www.zlib.net/
;
; example compressing a file "myfile"
; (write-file "myfile.z" (squeeze (read-file "myfile")))
;
; example un-compressing a file "myfile.z"
;           (write-file "myfile" (unsqueeze (read-file "myfile.z)))
;

; for Mac OS X
(import "libz.dylib" "compress")
(import "libz.dylib" "uncompress")

; compresses a string buffer in src and returns a compressed buffer
;
(define (squeeze src)
(letn ( (srclen (length src))
(destlen (int (add (mul 1.01 srclen) 12)))
(dest (dup "00" destlen))
(destlenp (pack "ld" destlen))
)
(compress dest destlenp src srclen)
(set 'destlen (first (unpack "ld" destlenp)))
(slice dest 0 destlen)))

; un-compresses a string buffer in src and returns the original
;
(define (unsqueeze src)
(letn ( (srclen (length src))
(destlen (* srclen 3))
(dest (dup "00" destlen))
(destlenp (pack "ld" destlen))
)
(while (= -5 (uncompress dest destlenp src srclen))
(set 'destlen (* 2 destlen))
(set 'dest (dup "00" destlen))
(set 'destlenp (pack "ld" destlen)))
(set 'destlen (first (unpack "ld" destlenp)))
(slice dest 0 destlen)))
 
;; eof


Lutz



ps: the code is also posted here: http://newlisp.org/index.cgi?Tips_and_Tricks">http://newlisp.org/index.cgi?Tips_and_Tricks

pjot

#3
Impressive!



For Linux only this change must be made:



(import "libz.so" "compress")
(import "libz.so" "uncompress")


Extremely fast, compresses and decompresses 2Mb in less than a second.



Peter

Lutz

#4
Added .gz compatible file compression/decompression. This allows to read and write .gz files. See: http://newlisp.org/code/zlib.lsp.txt">http://newlisp.org/code/zlib.lsp.txt



This file will also be part of the newlisp-x.x.x/modules directory aand bne installed in /usr/share/newlisp on UNIX systems when installing newLISP.



Lutz

lispen

#5
Wow, thats perfect Lutz. .gz compatible compression is exactly what I needed.  I just noticed you were in Boca Raton. I'm in West Palm Beach, you could have just driven that code over to me. :-)





Also thanks HPW.. I'm sure I'll use your hpwNLUtility somewhere soon.



Thanks for you help.



Ken