Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #' Extract the CRC (32 bit hash) of a gzip file
  2. #'
  3. #' @description Reads the crc from a gzip file, assuming it is the last 4 bytes
  4. #' of the file. First checks for a valid gzip magic number at the start of the
  5. #' file.
  6. #' @details CRC32 is not a strong hash like SHA1 or even MD5, but it does
  7. #' provide a basic hash of the \bold{uncompressed contents} of the gzip file.
  8. #' NB CRCs are stored in little endian byte order regardless of platform.
  9. #' @param f Path to a gzip file
  10. #' @return hexadecimal formatted
  11. #' @export
  12. #' @examples
  13. #' rdsfile=system.file('help/aliases.rds')
  14. #' gzip.crc(rdsfile)
  15. gzip.crc<-function(f){
  16. con=file(f,open='rb')
  17. on.exit(close(con),add=TRUE)
  18. magic=readBin(con,what='raw',n=2)
  19. if(magic[1]!=0x1f || magic[2]!=0x8b) {
  20. warning("This is not a gzip file")
  21. return(NA)
  22. }
  23. seek(con,-8,origin='end')
  24. # nb gzip always writes little endian
  25. crc=readBin(con,integer(),size=4, endian = 'little')
  26. format(as.hexmode(crc),width=8)
  27. }
  28.  
  29. #' Check if a file is a gzip file
  30. #'
  31. #' @param f Path to file to test
  32. #' @return logical indicating whether \code{f} is in gzip format (or \code{NA}
  33. #' if the file cannot be accessed)
  34. #' @export
  35. #' @examples
  36. #' notgzipfile=tempfile()
  37. #' writeLines('not a gzip', notgzipfile)
  38. #' is.gzip(notgzipfile)
  39. #' con=gzfile(gzipfile<-tempfile(),open='wt')
  40. #' writeLines('This one is gzipped', con)
  41. #' close(con)
  42. #' is.gzip(gzipfile)
  43. #' unlink(c(notgzipfile,gzipfile))
  44. is.gzip<-function(f) {
  45. if(file.access(f, mode=4)<0) return(NA)
  46. x=file(f)
  47. on.exit(close(x))
  48. isTRUE(summary(x)$class=='gzfile')
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement