Это действительно большой вариант использования функции xmlEventParse
в пакете XML
. Это файл размером 200+ Мбайт, и последнее, что вы хотите сделать, - ненужная потеря памяти (разбор XML - это, как известно, интенсивный объем памяти) и время от времени тратится через узлы несколько раз.
Используя xmlEventParse
, вы можете также фильтруйте то, что вы делаете или не нуждаетесь, и вы также можете получить индикатор прокрутки там, чтобы вы могли видеть, что происходит.
library(XML)
library(data.table)
# get the # of <rows> quickly; you can approximate if you don't know the
# number or can't run this and then chop down the size of the data.frame
# afterwards
system("grep -c '<row' ~/Desktop/p1.xml")
## 128010
n <- 128010
# pre-populate a data.frame
# you could also just write this data out to a file and read it back in
# which would negate the need to use global variables or pre-allocate
# a data.frame
dat <- data.frame(id=rep(NA_character_, n),
post_type_id=rep(NA_character_, n),
stringsAsFactors=FALSE)
# setup a progress bar since there are alot of nodes
pb <- txtProgressBar(min=0, max=n, style=3)
# this function will be called for each <row>
# again, you could write to a file/database/whatever vs do this
# data.frame population
idx <- 1
process_row <- function(node, tribs) {
# update the progress bar
setTxtProgressBar(pb, idx)
# get our data (you can filter here)
dat[idx, "id"] <<- tribs["Id"]
dat[idx, "post_type_id"] <<- tribs["PostTypeId"]
# update the index
idx <<- idx + 1
}
# start the parser
info <- xmlEventParse("Posts.xml", list(row=process_row))
# close up the progress bar
close(pb)
head(dat)
## id post_type_id
## 1 1 1
## 2 2 1
## 3 3 1
## 4 4 1
## 5 5 2
## 6 6 1
Можно использовать java.util.zip Java. ZipOutputStream для создания zip-файла в памяти. Например:
public static byte[] zipBytes(String filename, byte[] input) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry entry = new ZipEntry(filename);
entry.setSize(input.length);
zos.putNextEntry(entry);
zos.write(input);
zos.closeEntry();
zos.close();
return baos.toByteArray();
}
Возможно, java.util.zip пакет мог бы помочь Вам
, Так как Вы спрашиваете о том, как преобразовать из массива байтов, я думаю (не протестированный), можно использовать метод ByteArrayInputStream
int read(byte[] b, int off, int len)
Reads up to len bytes of data into an array of bytes from this input stream.
, что Вы будете питаться к
ZipInputStream This class implements an input stream filter for reading files in the ZIP file format.
Необходимо использовать ZipOutputStream для этого.
http://java.sun.com/javase/6/docs/api/java/util/zip/ZipOutputStream.html