newLISP Fan Club

Forum => newLISP and the O.S. => Topic started by: lispen on April 11, 2006, 03:37:13 PM

Title: Newlisp / Zlib
Post by: lispen on April 11, 2006, 03:37:13 PM
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/



Thanks,

Ken
Title:
Post by: HPW on April 11, 2006, 10:21:49 PM
Hello lispen,



Isn't jpg compressed enough?



Anyway, you may have a look at:



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



(import "hpwNLUtility.dll" "hpwZlibCompressFile")

(get-string(hpwZlibCompressFile "mySource.ext" "myTarget.ext"))
Title:
Post by: Lutz on April 12, 2006, 08:28:13 AM
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
Title:
Post by: pjot on April 12, 2006, 09:36:35 AM
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
Title:
Post by: Lutz on April 13, 2006, 09:14:06 AM
Added .gz compatible file compression/decompression. This allows to read and write .gz files. See: 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
Title:
Post by: lispen on April 13, 2006, 11:34:54 PM
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