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 710283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:36:13+00:00 2026-05-14T04:36:13+00:00

I had created a zip file (together with directory) under Windows as follow (Code

  • 0

I had created a zip file (together with directory) under Windows as follow (Code are picked from http://www.exampledepot.com/egs/java.util.zip/CreateZip.html) :

package sandbox;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 *
 * @author yan-cheng.cheok
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // These are the files to include in the ZIP file
        String[] filenames = new String[]{"MyDirectory" + File.separator + "MyFile.txt"};

        // Create a buffer for reading the files
        byte[] buf = new byte[1024];

        try {
            // Create the ZIP file
            String outFilename = "outfile.zip";
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

            // Compress the files
            for (int i=0; i<filenames.length; i++) {
                FileInputStream in = new FileInputStream(filenames[i]);

                // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(filenames[i]));

                // Transfer bytes from the file to the ZIP file
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Complete the entry
                out.closeEntry();
                in.close();
            }

            // Complete the ZIP file
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

The newly created zip file can be extracted without problem under Windows, by using http://www.exampledepot.com/egs/java.util.zip/GetZip.html

However, I realize if I extract the newly created zip file under Linux, using modified version of http://www.exampledepot.com/egs/java.util.zip/GetZip.html. The original version doesn’t check for directory using zipEntry.isDirectory()).

public static boolean extractZipFile(File zipFilePath, boolean overwrite) {
    InputStream inputStream = null;
    ZipInputStream zipInputStream = null;
    boolean status = true;

    try {
        inputStream = new FileInputStream(zipFilePath);

        zipInputStream = new ZipInputStream(inputStream);
        final byte[] data = new byte[1024];

        while (true) {
            ZipEntry zipEntry = null;
            FileOutputStream outputStream = null;

            try {
                zipEntry = zipInputStream.getNextEntry();

                if (zipEntry == null) break;

                final String destination = Utils.getUserDataDirectory() + zipEntry.getName();

                if (overwrite == false) {
                    if (Utils.isFileOrDirectoryExist(destination)) continue;
                }

                if (zipEntry.isDirectory())
                {
                    Utils.createCompleteDirectoryHierarchyIfDoesNotExist(destination);
                }
                else
                {
                    final File file = new File(destination);
                    // Ensure directory is there before we write the file.
                    Utils.createCompleteDirectoryHierarchyIfDoesNotExist(file.getParentFile());

                    int size = zipInputStream.read(data);

                    if (size > 0) {
                        outputStream = new FileOutputStream(destination);

                        do {
                            outputStream.write(data, 0, size);
                            size = zipInputStream.read(data);
                        } while(size >= 0);
                    }
                }
            }
            catch (IOException exp) {
                log.error(null, exp);
                status = false;
                break;
            }
            finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    }
                    catch (IOException exp) {
                        log.error(null, exp);
                        break;
                    }
                }

                if (zipInputStream != null) {
                    try {
                        zipInputStream.closeEntry();
                    }
                    catch (IOException exp) {
                        log.error(null, exp);
                        break;
                    }
                }
            }

        }   // while(true)
    }
    catch (IOException exp) {
        log.error(null, exp);
        status = false;
    }
    finally {
        if (zipInputStream != null) {
            try {
                zipInputStream.close();
            } catch (IOException ex) {
                log.error(null, ex);
            }
        }

        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ex) {
                log.error(null, ex);
            }
        }
    }
    return status;
}

“MyDirectory\MyFile.txt” instead of MyFile.txt being placed under folder MyDirectory.

I try to solve the problem by changing the zip file creation code to

    String[] filenames = new String[]{"MyDirectory" + "/" + "MyFile.txt"};

But, is this an eligible solution, by hard-coded the seperator? Will it work under Mac OS? (I do not have a Mac to try out)

  • 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-14T04:36:14+00:00Added an answer on May 14, 2026 at 4:36 am

    Yes, your solution (though apparently inelegant) is the right way.
    The “/” should be used inside zipentry, not the local File.separator

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doug McCune had created something that was exactly what I needed ( http://dougmccune.com/blog/2007/05/10/analyze-your-actionscript-code-with-this-apollo-app/ )
I had a plugin installed in Visual Studio 2008, and it created some extra
On my previous machine I had IIS6 installed and created a web project using
I use Codesmith to create our code generation templates and have had success in
I created a FileSystemWatcher (as a Windows Service) to watch for plug-ins being dropped
I'm trying to put together a zip streaming solution through the use of Unix's
My problem is: I had created a new user: GRANT ALL PRIVILAGES ON sampdb.*
I have a custom control that I had created I need to host it
On the project I am currently working, I had created an action that generates
i registered my app and i got client_id,secret_id,etc.. ( http://developers.facebook.com/setup/ ) i had already

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.