Ooops
November 25, 2020, 05:24:25 AM

Author Topic: [bmx] Read contents of online zip file by BlitzSupport [ 1+ years ago ]  (Read 703 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Read contents of online zip file
Author : BlitzSupport
Posted : 1+ years ago

Description : This is just a brutal port (via 'Import BB') of <a href="codearcs0f8e.html?code=1001" >readzip[/url] by Mike Carroll to BlitzMax, with online support hacked in.

I wanted to see the contents of the latest Android SDK to see if there was anything new compared to my install, and found Mike's code in the Code Archives -- all credit for this goes to him!

This example just prints the contents of <a href="../toolbox/toolboxa8fb.html?tool=61" target="_blank">the Gnet zip archive[/url] to the command line (or IDE output).


Code :
Code: BlitzMax
  1. ' BMX/online port of...
  2.  
  3. ' readzip.bb
  4. ' by Mike Carroll, 20040418
  5. ' www.iol.ie/~carrollm
  6.  
  7. ' Need to explore the contents of a zip file? You do? Then you're in luck! With
  8. ' Unky Mike's handy Zip File reader, your Blitz Basic program need never again
  9. ' suffer from zip-file-contents-ignorance!
  10. '
  11. ' Note: this function DOES NOT extract the contents of a zip file; it just tells
  12. ' you what's in there.
  13. '
  14. ' The routine was written using Unky Mike's MIUAYGA principle ("Making It Up As"
  15. ' You Go Along"); it works fine on every zip file I've tried, but it's possible"
  16. ' that there are files out there for which it won't work...
  17. '
  18. ' The accompanying functions are as follows:
  19. '
  20. ' FileDateToStr(fd) & FileTimeToStr(ft)
  21. ' Turns the filedate and filetime values into human-readable formats. Filedates
  22. ' and filetimes are squashed into two-byte integers in the zip file (in the same
  23. ' format as DOS used to use).
  24. '
  25. ' Pad0(i, w)
  26. ' Pads a number with leading zeroes. In this instance, the function is used only
  27. ' for displaying the extracted dates and times, but please feel free to use it
  28. ' anywhere you see fit.
  29. '
  30. ' Del$(st$, pos, num)
  31. ' Deletes the specified number of characters from the string, starting at the
  32. ' specified position. Not used in this case, but I find it really handy so I
  33. ' decided to give it away free!
  34. '
  35. ' Ins$(st1$, st2$, pos)
  36. ' Inserts st2$ into st1$ at the specified position. Also not used in this case,
  37. ' but it's also really handy and anyway the Del$ function absolutely refused to
  38. ' go without it.
  39. '
  40.  
  41. file$ = "http://www.blitzbasic.com/gnet/gnet_v1.zip"
  42.  
  43. ReadDataZipfile file
  44.  
  45. End
  46.  
  47. Function OnlineSeekAhead (s:TStream, offset)
  48.         For bytes = 0 Until offset
  49.                 ReadByte s
  50.         Next
  51. End Function
  52.  
  53. Function ReadDataZipfile(filename$)
  54.  
  55. ' Note: there's not much done in the way of checking: the function returns 1 if the file exists, 0 if it doesn't
  56.  
  57.         If Lower (Left (filename, 7)) <> "http://" Then Return
  58.        
  59.         filename = "http::" + Right (filename, Len (filename) - 7)
  60.        
  61.         filein:TStream = LittleEndianStream (ReadStream (filename$))
  62.  
  63.         If filein <> Null Then ' the file exists
  64.                
  65.                 Try
  66.                
  67.                 While (Not Eof(filein))
  68.                
  69.                         ' read the header signature to determine whether this is a file or a central directory record
  70.                         headersig = ReadInt(filein)
  71.                        
  72.                         If headersig = $04034b50 Then ' this is a file  (0x04034b50) = 67324752
  73.                                 version = ReadByte(filein) + 256*(ReadByte(filein))
  74.                                 generalpurpose = ReadByte(filein) + 256*(ReadByte(filein))
  75.                                 compmethod = ReadByte(filein) + 256*(ReadByte(filein))
  76.                                 modtime = ReadByte(filein) + (256 * ReadByte(filein))
  77.                                 moddate = ReadByte(filein) + (256 * ReadByte(filein))
  78.                                 crc32 = ReadInt(filein)
  79.                                 compsize = ReadInt(filein)
  80.                                 uncompsize = ReadInt(filein)
  81.                                 fnlen = ReadByte(filein) + 256*(ReadByte(filein))
  82.                                 extrafieldlen = ReadByte(filein) + 256*(ReadByte(filein))
  83.                
  84.                                 fn$ = ""
  85.                                 For i = 1 To fnlen
  86.                                         fn = fn + Chr(ReadByte(filein))
  87.                                 Next
  88.                        
  89.                                 ' skip file data and extra field data
  90.                         '       SeekStream filein, StreamPos(filein)+compsize+extrafieldlen
  91.                                
  92.                                 OnlineSeekAhead filein, compsize+extrafieldlen
  93.  
  94.                                 If generalpurpose & 8 Then
  95.                                         ' If generalpurpose bit 3 is set, then CRC, compsize and uncompsize are incorrect in the header...
  96.                                         crc32 = ReadByte(filein)+ReadByte(filein)+ReadByte(filein)+ReadByte(filein)
  97.                                         compsize = ReadInt(filein)
  98.                                         uncompsize = ReadInt(filein)
  99.                                 EndIf
  100.  
  101.                                 count = count + 1
  102.                                 Print count+"   "+fn$+"  "+uncompsize+"  "+compsize+"  "+FileDateToStr(moddate)+"  "+FileTimeToStr(modtime)
  103.                         EndIf
  104.                
  105.                         If headersig = $02014b50 Then' this is a central directory record  (0x02014b50) = 33639248
  106.                                 versionmadeby = ReadByte(filein) + 256*(ReadByte(filein))
  107.                                 version = ReadByte(filein) + 256*(ReadByte(filein))
  108.                                 generalpurpose = ReadByte(filein) + 256*(ReadByte(filein))
  109.                                 compmethod = ReadByte(filein) + 256*(ReadByte(filein))
  110.                                 modtime = ReadByte(filein) + (256 * ReadByte(filein))
  111.                                 moddate = ReadByte(filein) + (256 * ReadByte(filein))
  112.                                 crc32 = ReadInt(filein)
  113.                                 compsize = ReadInt(filein)
  114.                                 uncompsize = ReadInt(filein)
  115.                                 fnlen = ReadByte(filein) + 256*(ReadByte(filein))
  116.                                 extrafieldlen = ReadByte(filein) + 256*(ReadByte(filein))
  117.                                 commentlen = ReadByte(filein) + 256*(ReadByte(filein))
  118.                                 disknumberstart = ReadByte(filein) + 256*(ReadByte(filein))
  119.                                 internalfileattributes = ReadByte(filein) + 256*(ReadByte(filein))
  120.                                 externalfileattributes = ReadInt(filein)
  121.                                 relativeoffsetoflocalheader = ReadInt(filein)
  122.                                        
  123.                                 ' skip file data and extra field data
  124.                                 'SeekStream filein, StreamPos(filein)+compsize+extrafieldlen
  125.                                 OnlineSeekAhead filein, compsize+extrafieldlen
  126.  
  127.                                 endofcentraldirsignature     = ReadInt(filein) ' (0x06054b50) = 101010256
  128.                                 numberofthisdisk             = ReadByte(filein) + 256*(ReadByte(filein))
  129.                                 numberofdiskwithstart            = ReadByte(filein) + 256*(ReadByte(filein))
  130.                                 totalentriesincentraldirondisk = ReadByte(filein) + 256*(ReadByte(filein))
  131.                                 totalentriesincentraldir       = ReadByte(filein) + 256*(ReadByte(filein))
  132.                                 sizeofthecentraldir   = ReadInt(filein)
  133.                                 offsetofcentraldir    = ReadInt(filein)
  134.                                 commentlen        = ReadByte(filein) + 256*(ReadByte(filein))
  135.                                 comment$ = ""
  136.                                 For i = 1 To commentlen        
  137.                                         comment = comment + Chr(ReadByte(filein))
  138.                                 Next
  139.                         EndIf
  140.                        
  141.                 Wend
  142.                
  143.                 Catch ReadFail:Object
  144.                         DebugLog "Read error in " + filename
  145.                        
  146.                 End Try
  147.                
  148.                 CloseFile(filein)
  149.                 Return 1
  150.  
  151.         Else ' the file doesn't exist, so return 0
  152.                 Return 0
  153.         EndIf
  154. End Function' ReadZipfile
  155.  
  156.  
  157.  
  158. Function FileDateToStr$(fd)
  159. ' filedate = ((year - 1980) * 512) + (month * 32) + day
  160. ' Returns "yyyy/mm/dd"
  161.         Local year, month, day
  162.         year = (fd / 512)+1980
  163.         month = (fd Mod 512) / 32
  164.         day = (fd Mod 32)
  165.         Return year+"/"+ Pad0(month, 2) + "/"+ Pad0(day, 2)
  166. End Function' FileDateToStr
  167.  
  168.  
  169. Function FileTimeToStr$(ft)
  170. ' filetime = (hour * 2048) + (min * 32) + (sec / 2)
  171. ' Returns "hh:mm:ss"
  172.         Local hour, fmin, sec
  173.         hour = (ft / 2048)
  174.         fmin = (ft Mod 2048) / 32
  175.         sec = (ft Mod 32) * 2
  176.         Return Pad0(hour, 2)+":"+Pad0(fmin, 2)+":"+Pad0(sec, 2)
  177. End Function' FileTimeToStr
  178.  
  179.  
  180. Function Pad0$(i, w)
  181. ' Uses leading zeros to pad a number to the required length
  182.         Local tst$
  183.         tst$ = i
  184.         While Len(tst) < w
  185.                 tst = "0"+tst
  186.         Wend
  187.         Return tst
  188. End Function' Pad0
  189.  
  190.  
  191. Function Del$(st$, pos, num)
  192. ' Deletes 'num' characters from the string, starting at the position specified by 'pos'
  193.         Local tst$
  194.         tst$ = Left$(st, pos-1)
  195.         tst2$ = Right$(st, Len(st)-(pos+num-1))
  196.         Return tst$ + tst2$
  197. End Function
  198.  
  199.  
  200. Function Ins$(st1$, st2$, pos)
  201. ' Inserts st2 into st1 at the position specified by 'pos'
  202.         Local tst$
  203.         tst$ = Left$(st1, pos-1)
  204.         tst2$ = Right$(st1, Len(st1)-(pos-1))
  205.         Return tst+st2+tst2
  206. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal