home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / TEST_GZIP.PY < prev    next >
Encoding:
Text File  |  2000-09-28  |  1.2 KB  |  55 lines

  1.  
  2. import sys, os
  3. import gzip, tempfile
  4.  
  5. filename = tempfile.mktemp()
  6.  
  7. data1 = """  int length=DEFAULTALLOC, err = Z_OK;
  8.   PyObject *RetVal;
  9.   int flushmode = Z_FINISH;
  10.   unsigned long start_total_out;
  11.  
  12. """
  13.  
  14. data2 = """/* zlibmodule.c -- gzip-compatible data compression */
  15. /* See http://www.cdrom.com/pub/infozip/zlib/ */
  16. /* See http://www.winimage.com/zLibDll for Windows */
  17. """
  18.  
  19. f = gzip.GzipFile(filename, 'wb') ; f.write(data1 * 50) ; f.close()
  20.  
  21. f = gzip.GzipFile(filename, 'rb') ; d = f.read() ; f.close()
  22. assert d == data1*50
  23.  
  24. # Append to the previous file
  25. f = gzip.GzipFile(filename, 'ab') ; f.write(data2 * 15) ; f.close()
  26.  
  27. f = gzip.GzipFile(filename, 'rb') ; d = f.read() ; f.close()
  28. assert d == (data1*50) + (data2*15)
  29.  
  30. # Try .readline() with varying line lengths
  31.  
  32. f = gzip.GzipFile(filename, 'rb')
  33. line_length = 0
  34. while 1:
  35.     L = f.readline( line_length )
  36.     if L == "" and line_length != 0: break
  37.     assert len(L) <= line_length
  38.     line_length = (line_length + 1) % 50
  39. f.close()
  40.  
  41. # Try .readlines() 
  42.  
  43. f = gzip.GzipFile(filename, 'rb')
  44. L = f.readlines()
  45. f.close()
  46.  
  47. f = gzip.GzipFile(filename, 'rb')
  48. while 1:
  49.     L = f.readlines(150)
  50.     if L == []: break    
  51. f.close()
  52.  
  53.  
  54. os.unlink( filename )
  55.