0

Currently I have a .gz file of a csv file. And I would like to uncompressed it.

The difficulty is that after decompressing the .gz file, it needs a bitewise operation to get it to the right format.

I have the code in python that works, but I am struggling to convert it to R

def uncompress_file(file_location_gz, file_location_save):

    with open(file_location_gz, "rb") as f:
      data = f.read()
    data2 = gzip.GzipFile(fileobj=BytesIO(data),
                         mode='rb').read()
    data2 = bytearray(data2)
    for i in range(len(data2)):
      data2[i] ^= 0x95
    with open(file_location_save, 'wb') as f_out:
     f_out.write(data2)

Any help or suggestion would be really helpful.

dwt
  • 1
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 18 '20 at 22:27

1 Answers1

1

Here's what the function might look like in R

decompress_xor <- function(file_in, file_out, xor_byte = 0x95) {
  gfin <- gzfile(file_in, "rb")
  bytes <- readBin(gfin, "raw", file.info(file_in)$size)
  close(gfin)
  decoded_bytes <- as.raw(bitwXor(as.numeric(bytes), xor_byte))
  rawToChar(decoded_bytes)
  writeBin(decoded_bytes, file_out)
}

We use gzfile() to take care of the decompression, then read the data as raw bytes. We transform those bytes using xor, then write the bytes back out.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • thank you very much @MrFlick it worked!! I have been stuck on this for some time. Thank you again. – dwt Oct 18 '20 at 23:06