我想将 EMR 本地文件系统中的目录作为压缩文件上传到 s3。
有没有比我目前使用的方法更好的方法来解决这个问题?
是否可以将 ZipOutputStream 作为 Reducer 输出返回?
谢谢
zipFolderAndUpload("target", "target.zip", "s3n://bucketpath/");
static public void zipFolderAndUpload(String srcFolder, String zipFile, String dst) throws Exception {
//Zips a directory
FileOutputStream fileWriter = new FileOutputStream(zipFile);
ZipOutputStream zip = new ZipOutputStream(fileWriter);
addFolderToZip("", srcFolder, zip);
zip.flush();
zip.close();
// Copies the zipped file to the s3 filesystem,
InputStream in = new BufferedInputStream(new FileInputStream(zipFile));
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst+zip), conf);
OutputStream out = fs.create(new Path(dst+zip));
IOUtils.copyBytes(in, out, 4096, true);
}
static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {
File folder = new File(srcFile);
if (folder.isDirectory()) {
addFolderToZip(path, srcFile, zip);
} else {
byte[] buf = new byte[1024];
int len;
FileInputStream in = new FileInputStream(srcFile);
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
}
}
static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception {
File folder = new File(srcFolder);
for (String fileName : folder.list()) {
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
} else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
}
}
}