如何在appengine java应用程序中创建包含Google云存储对象的zip存档? [英] How to create a zip archive containing google cloud storage objects within appengine java app?
本文介绍了如何在appengine java应用程序中创建包含Google云存储对象的zip存档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该如何从appengine的java应用程序中做到这一点?
解决方案
我写了下面的方法似乎是工作正常。
public static void zipFiles(final GcsFilename targetZipFile,
final GcsFilename ... filesToZip)throws IOException {
Preconditions.checkArgument(targetZipFile!= null);
Preconditions.checkArgument(filesToZip!= null);
Preconditions.checkArgument(filesToZip.length> 0);
final int fetchSize = 4 * 1024 * 1024;
final int readSize = 2 * 1024 * 1024;
GcsOutputChannel outputChannel = null;
ZipOutputStream zip = null;
try {
GcsFileOptions options = new GcsFileOptions.Builder()。mimeType(MediaType.ZIP.toString())。build();
outputChannel = GCS_SERVICE.createOrReplace(targetZipFile,options);
zip = new ZipOutputStream(Channels.newOutputStream(outputChannel));
GcsInputChannel readChannel = null;
for(GcsFilename file:filesToZip){
try {
final GcsFileMetadata meta = GCS_SERVICE.getMetadata(file);
if(meta == null){
LOGGER.warning(file.toString()+NOT FOUND。Skipping。);
继续;
}
// int fileSize =(int)meta.getLength();
// LOGGER.fine(adding+ file.toString());
ZipEntry entry = new ZipEntry(file.getObjectName());
zip.putNextEntry(entry);
readChannel = GCS_SERVICE.openPrefetchingReadChannel(file,0,fetchSize);
final ByteBuffer buffer = ByteBuffer.allocate(readSize);
int bytesRead = 0;
while(bytesRead> = 0){
bytesRead = readChannel.read(buffer);
buffer.flip();
zip.write(buffer.array(),buffer.position(),buffer.limit());
buffer.rewind();
buffer.limit(buffer.capacity());
}
} finally {
zip.closeEntry();
readChannel.close();
}
}
}最后{
zip.flush();
zip.close();
outputChannel.close();
}
}
Let's say I have 50 objects (15Mb each) stored in Google Cloud Storage. Now I need to create a zip archive containing all of them and store the resultant file back at GCS. How can I do that from within an appengine java app?
解决方案
I wrote the method below which seems to be working fine.
public static void zipFiles(final GcsFilename targetZipFile,
final GcsFilename... filesToZip) throws IOException {
Preconditions.checkArgument(targetZipFile != null);
Preconditions.checkArgument(filesToZip != null);
Preconditions.checkArgument(filesToZip.length > 0);
final int fetchSize = 4 * 1024 * 1024;
final int readSize = 2 * 1024 * 1024;
GcsOutputChannel outputChannel = null;
ZipOutputStream zip = null;
try {
GcsFileOptions options = new GcsFileOptions.Builder().mimeType(MediaType.ZIP.toString()).build();
outputChannel = GCS_SERVICE.createOrReplace(targetZipFile, options);
zip = new ZipOutputStream(Channels.newOutputStream(outputChannel));
GcsInputChannel readChannel = null;
for (GcsFilename file : filesToZip) {
try {
final GcsFileMetadata meta = GCS_SERVICE.getMetadata(file);
if (meta == null) {
LOGGER.warning(file.toString() + " NOT FOUND. Skipping.");
continue;
}
//int fileSize = (int) meta.getLength();
// LOGGER.fine("adding " + file.toString());
ZipEntry entry = new ZipEntry(file.getObjectName());
zip.putNextEntry(entry);
readChannel = GCS_SERVICE.openPrefetchingReadChannel(file, 0, fetchSize);
final ByteBuffer buffer = ByteBuffer.allocate(readSize);
int bytesRead = 0;
while (bytesRead >= 0) {
bytesRead = readChannel.read(buffer);
buffer.flip();
zip.write(buffer.array(), buffer.position(), buffer.limit());
buffer.rewind();
buffer.limit(buffer.capacity());
}
} finally {
zip.closeEntry();
readChannel.close();
}
}
} finally {
zip.flush();
zip.close();
outputChannel.close();
}
}
这篇关于如何在appengine java应用程序中创建包含Google云存储对象的zip存档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文