Newlisp / Zlib

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
lispen
Posts: 2
Joined: Tue Apr 11, 2006 10:31 pm

Newlisp / Zlib

Post 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

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post 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"))
Hans-Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

lispen
Posts: 2
Joined: Tue Apr 11, 2006 10:31 pm

Post 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

Locked