在R中解压缩gz文件,并执行位操作

2024-10-04 05:24:36 发布

您现在位置:Python中文网/ 问答频道 /正文

目前我有一个csv文件的.gz文件。我想把它解压缩

困难在于解压.gz文件后,需要进行bitewise操作才能将其转换为正确的格式

我有python中的代码可以工作,但我正在努力将其转换为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)

任何帮助或建议都会非常有用


Tags: 文件csvreaddatasaveaswithlocation
1条回答
网友
1楼 · 发布于 2024-10-04 05:24:36

下面是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)
}

我们使用gzfile()进行解压缩,然后将数据作为原始字节读取。我们使用xor转换这些字节,然后将字节写回

相关问题 更多 >