Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 906907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:27:48+00:00 2026-05-15T16:27:48+00:00

Use Case I need to package up our kml which is in a String

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:27:49+00:00Added an answer on May 15, 2026 at 4:27 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build some client side code which follows this use case: An
I have a use case where I need to call a (non-static) method in
I have a use case where I only need to store certain fields to
General use case I am trying to implement a basic shell. Description I need
I have a strong use case for pre-allocating all the memory I need upfront
Use case: user clicks the link on a webpage - boom! load of files
I have a use case where I need to add information about the user
I've a use case where I need to create a rule dynamically for a
I need to use Spring Security 3 in my application which is composed by
I am using javadoc doclets with gradle, so I need to use the package

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.