【问题标题】:Writing Zip Files to GAE Blobstore将 Zip 文件写入 GAE Blobstore
【发布时间】:2011-12-25 23:46:47
【问题描述】:

我正在使用 Java API 读取和写入 Google App Engine Blobstore。

我需要将文件直接压缩到 Blobstore 中,这意味着我有 String 对象,我希望在压缩时将其存储在 Blobstore 中。

我的问题是标准压缩方法是使用OutputStream 写入,而 GAE 似乎没有提供写入 Blobstore 的方法。

有没有办法组合这些 API,或者我可以使用不同的 API(我还没有找到)?

【问题讨论】:

    标签: java google-app-engine zip blobstore


    【解决方案1】:

    如果我没记错的话,你可以试试Blobstore low level API。它提供了一个 Java Channel (FileWriteChannel),因此您可以将其转换为 OutputStream

    Channels.newOutputStream(channel)
    

    并将该输出流与您当前使用的 java.util.zip.* 类一起使用(here 您有一个相关示例,它使用 Java NIO 将某些内容压缩到 Channel/OutputStream

    我没试过。

    【讨论】:

    • 我看到你是这里的新用户。您应该将答案标记为已接受,它解决了您的问题。这对其他人也有帮助。
    【解决方案2】:

    这是一个编写内容文件并将其压缩并存储到 blobstore 的示例:

    AppEngineFile file = fileService.createNewBlobFile("application/zip","fileName.zip");
    
    try {
    
         FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
    
         //convert as outputstream
        OutputStream blobOutputStream = Channels.newOutputStream(writeChannel);
    
        ZipOutputStream zip = new ZipOutputStream(blobOutputStream);                    
    
         zip.putNextEntry(new ZipEntry("fileNameTozip.txt"));
    
         //read the content from your file or any context you want to get
         final byte data[] = IOUtils.toByteArray(file1InputStream);                    
    
         //write byte data[] to zip
          zip.write(bytes);
    
         zip.closeEntry();                    
         zip.close();
    
         // Now finalize
         writeChannel.closeFinally();
     } catch (IOException e) {
         throw new RuntimeException(" Writing file into blobStore", e);
     }
    

    【讨论】:

      【解决方案3】:

      另一个答案是使用 BlobStore api,但目前推荐的方法是使用 App Engine GCS 客户端。

      这是我用来在 GCS 中压缩多个文件的方法:

      public static void zipFiles(final GcsFilename targetZipFile,
                                  Collection<GcsFilename> filesToZip) throws IOException {
      
          final GcsFileOptions options = new GcsFileOptions.Builder()
                  .mimeType(MediaType.ZIP.toString()).build();
          try (GcsOutputChannel outputChannel = gcsService.createOrReplace(targetZipFile, options);
               OutputStream out = Channels.newOutputStream(outputChannel);
               ZipOutputStream zip = new ZipOutputStream(out)) {
      
              for (GcsFilename file : filesToZip) {
                  try (GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(file, 0, MB);
                       InputStream is = Channels.newInputStream(readChannel)) {
                      final GcsFileMetadata meta = gcsService.getMetadata(file);
                      if (meta == null) {
                          log.warn("{} NOT FOUND. Skipping.", file.toString());
                          continue;
                      }
                      final ZipEntry entry = new ZipEntry(file.getObjectName());
                      zip.putNextEntry(entry);
      
                      ByteStreams.copy(is, zip);
                      zip.closeEntry();
                  }
                  zip.flush();
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-11
        • 2011-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-18
        相关资源
        最近更新 更多