本帖最后由 highflybird 于 2011-7-27 21:12 编辑
ADODB.Stream
展示如何打包二进制文件和读写二进制文件。
-
- (defun c:test ()
- ;;Read a Binary file
- (defun ReadBinary (FileName / stream arr)
- (setq stream (vlax-create-object "ADODB.Stream"))
- (vlax-put stream 'type 1) ;adTypeBinary
- (vlax-invoke stream 'open) ;adModeRead =1 adModeWrite =2 adModeReadWrite =3
- (vlax-invoke stream 'LoadFromFile filename)
- (setq Arr (vlax-invoke-method stream 'read (vlax-get stream 'SIZE)));read stream
- (vlax-invoke stream 'close)
- (vlax-release-object stream)
- (vlax-safearray->list (vlax-variant-value arr)) ;if a large size file ,it will take a long time in this step
- )
- ;;Write to a Binary file from a text stream
- (defun WriteBinary (FileName Array / stream)
- (setq stream (vlax-create-object "ADODB.Stream"))
- (vlax-put stream 'type 1) ;adTypeBinary
- (vlax-invoke stream 'open) ;adModeRead =1 adModeWrite =2 adModeReadWrite =3
- (vlax-invoke-method stream 'Write array) ;write stream
- (vlax-invoke stream 'saveToFile fileName 2) ;save
- (vlax-invoke stream 'close)
- (vlax-release-object stream)
- )
- (setq path (getfiled "Please select a binary file:" "c:/" "" 8 )) ;get file path
- (setq f (open "C:\\test.txt" "W"))
- (setq data (readBinary path))
- (princ data f)
- (close F)
- ;;(setq stream (vl-get-resource "test")) ;we can wrap this text file into .vlx file
- (setq f (open "C:\\test.txt" "R")) ;open for read
- (setq l "")
- (while (setq s (read-line f))
- (setq l (strcat l s))
- )
- (setq array (read l))
- (close f)
-
- (setq dat (vlax-make-safearray 17 (cons 0 (1- (length array))))) ;17 for unsigned char
- (vlax-safearray-fill dat array)
- (setq bin (vlax-make-variant dat))
- (writeBinary "C:\\test.jpg" bin) ;write binary file.
- )
|