如何使用 java.util.zip 归档/压缩 java 中的字符串以便在 Google Earth 中使用?

发布于 2024-09-09 00:00:01 字数 2187 浏览 6 评论 0原文

用例

我需要将字符串中的 kml 打包成 Google Earth 中网络链接的 kmz 响应。我还想在我这样做的时候把图标之类的东西包起来。

问题

使用下面的实现,我收到来自 WinZip 和 Google Earth 的错误,分别表示存档已损坏或文件无法打开。与我构建的其他示例不同的部分是添加字符串的行:

ZipEntry kmlZipEntry = new ZipEntry("doc.kml");
out.putNextEntry(kmlZipEntry);
out.write(kml.getBytes("UTF-8"));

请指出正确的方向以正确编写字符串,以便它位于 doc.xml 中生成的 kmz 文件。我知道如何将字符串写入临时文件,但我非常希望将操作保留在内存中,以便于理解和提高效率。

    private static final int BUFFER = 2048;
    private static void kmz(OutputStream os, String kml)
    {
        try{
            BufferedInputStream origin = null;
            ZipOutputStream out = new ZipOutputStream(os);
            out.setMethod(ZipOutputStream.DEFLATED);
            byte data[] = new byte[BUFFER];
            File f = new File("./icons"); //folder containing icons and such
            String files[] = f.list();

            if(files != null)
            {
                for (String file: files) {
                    LOGGER.info("Adding to KMZ: "+ file);
                    FileInputStream fi = new FileInputStream(file);
                    origin = new BufferedInputStream(fi, BUFFER);
                    ZipEntry entry = new ZipEntry(file);
                    out.putNextEntry(entry);
                    int count;
                    while((count = origin.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, count);
                    }
                    origin.close();
                }
            }
            ZipEntry kmlZipEntry = new ZipEntry("doc.kml");
            out.putNextEntry(kmlZipEntry);
            out.write(kml.getBytes("UTF-8"));
        }
        catch(Exception e)
        {
            LOGGER.error("Problem creating kmz file", e);
        }
    }

向我展示如何将 icons 文件夹中的补充文件放入存档中的类似文件夹中而不是与 doc.kml 位于同一层的奖励积分。

更新 即使将字符串保存到临时文件也会发生错误。啊。

用例注释 该用例用于 Web 应用程序,但获取文件列表的代码在那里不起作用。有关详细信息,请参阅 how-to-access-local-files -jboss-application 中的服务器

Use Case

I need to package up our kml which is in a String into a kmz response for a network link in Google Earth. I would like to also wrap up icons and such while I'm at it.

Problem

Using the implementation below I receive errors from both WinZip and Google Earth that the archive is corrupted or that the file cannot be opened respectively. The part that deviates from other examples I'd built this from are the lines where the string is added:

ZipEntry kmlZipEntry = new ZipEntry("doc.kml");
out.putNextEntry(kmlZipEntry);
out.write(kml.getBytes("UTF-8"));

Please point me in the right direction to correctly write the string so that it is in doc.xml in the resulting kmz file. I know how to write the string to a temporary file, but I would very much like to keep the operation in memory for understandability and efficiency.

    private static final int BUFFER = 2048;
    private static void kmz(OutputStream os, String kml)
    {
        try{
            BufferedInputStream origin = null;
            ZipOutputStream out = new ZipOutputStream(os);
            out.setMethod(ZipOutputStream.DEFLATED);
            byte data[] = new byte[BUFFER];
            File f = new File("./icons"); //folder containing icons and such
            String files[] = f.list();

            if(files != null)
            {
                for (String file: files) {
                    LOGGER.info("Adding to KMZ: "+ file);
                    FileInputStream fi = new FileInputStream(file);
                    origin = new BufferedInputStream(fi, BUFFER);
                    ZipEntry entry = new ZipEntry(file);
                    out.putNextEntry(entry);
                    int count;
                    while((count = origin.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, count);
                    }
                    origin.close();
                }
            }
            ZipEntry kmlZipEntry = new ZipEntry("doc.kml");
            out.putNextEntry(kmlZipEntry);
            out.write(kml.getBytes("UTF-8"));
        }
        catch(Exception e)
        {
            LOGGER.error("Problem creating kmz file", e);
        }
    }

Bonus points for showing me how to put the supplementary files from the icons folder into a similar folder within the archive as opposed to at the same layer as the doc.kml.

Update Even when saving the string to a temp file the errors occur. Ugh.

Use Case Note The use case is for use in a web app, but the code to get the list of files won't work there. For details see how-to-access-local-files-on-server-in-jboss-application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转角预定愛 2024-09-16 00:00:01

您忘记对 ZipOutputStream 调用 close()。调用它的最佳位置是创建它的 try 块的 finally 块。


更新:要创建文件夹,只需在条目名称中添加其名称即可。

ZipEntry entry = new ZipEntry("icons/" + file);

You forgot to call close() on ZipOutputStream. Best place to call it is the finally block of the try block where it's been created.


Update: To create a folder, just prepend its name in the entry name.

ZipEntry entry = new ZipEntry("icons/" + file);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文