Page 1 of 1

Newlisp / Zlib

Posted: Tue Apr 11, 2006 10:37 pm
by 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/

Thanks,
Ken

Posted: Wed Apr 12, 2006 5:21 am
by HPW
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"))

Posted: Wed Apr 12, 2006 3:28 pm
by Lutz
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

Code: Select all

; - 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 "\000" 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 "\000" destlen))
			(destlenp (pack "ld" destlen))
			)
		(while (= -5 (uncompress dest destlenp src srclen))
			(set 'destlen (* 2 destlen))	
			(set 'dest (dup "\000" 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

Posted: Wed Apr 12, 2006 4:36 pm
by pjot
Impressive!

For Linux only this change must be made:

Code: Select all

(import "libz.so" "compress")
(import "libz.so" "uncompress")
Extremely fast, compresses and decompresses 2Mb in less than a second.

Peter

Posted: Thu Apr 13, 2006 4:14 pm
by Lutz
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

Posted: Fri Apr 14, 2006 6:34 am
by lispen
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