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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:49:52+00:00 2026-05-15T03:49:52+00:00

I want to put some compressed data into a remote repository. To put data

  • 0

I want to put some compressed data into a remote repository.
To put data on this repository I can only use a method that take the name of the resource and its content as a String. (like data.txt + “hello world”).
The repository is moking a filesystem but is not, so I can not use File directly.

I want to be able to do the following:

  1. client send to server a file ‘data.txt’
  2. server compress ‘data.txt’ into a compressed file ‘data.zip’
  3. server send a string representation of data.zip to the repository
  4. repository store data.zip
  5. client download from repository data.zip and his able to open it with its favorite zip tool

The problem arise at step 3 when I try to get a string representation of my compressed file.

Here is a sample class, using the zip*stream and that emulate the repository showcasing my problem.
The created zip file is working, but after its ‘serialization’ it’s get corrupted.
(the sample class use jakarta commons.io )

Many thanks for your help.

package zip;

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

import org.apache.commons.io.FileUtils;

/**
 * Date: May 19, 2010 - 6:13:07 PM
 *
 * @author Guillaume AME.
 */
public class ZipMe {
    public static void addOrUpdate(File zipFile, File ... files) throws IOException {

        File tempFile = File.createTempFile(zipFile.getName(), null);
        // delete it, otherwise you cannot rename your existing zip to it.
        tempFile.delete();

        boolean renameOk = zipFile.renameTo(tempFile);
        if (!renameOk) {
            throw new RuntimeException("could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
        }
        byte[] buf = new byte[1024];

        ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

        ZipEntry entry = zin.getNextEntry();
        while (entry != null) {
            String name = entry.getName();
            boolean notInFiles = true;
            for (File f : files) {
                if (f.getName().equals(name)) {
                    notInFiles = false;
                    break;
                }
            }
            if (notInFiles) {
                // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(name));
                // Transfer bytes from the ZIP file to the output file
                int len;
                while ((len = zin.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            }
            entry = zin.getNextEntry();
        }
        // Close the streams
        zin.close();
        // Compress the files
        if (files != null) {
            for (File file : files) {
                InputStream in = new FileInputStream(file);
                // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(file.getName()));
                // 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
        }
        tempFile.delete();
        out.close();

    }

    public static void main(String[] args) throws IOException {

        final String zipArchivePath = "c:/temp/archive.zip";
        final String tempFilePath = "c:/temp/data.txt";
        final String resultZipFile = "c:/temp/resultingArchive.zip";

        File zipArchive = new File(zipArchivePath);
        FileUtils.touch(zipArchive);

        File tempFile = new File(tempFilePath);
        FileUtils.writeStringToFile(tempFile, "hello world");
        addOrUpdate(zipArchive, tempFile);

        //archive.zip exists and contains a compressed data.txt that can be read using winrar

        //now simulate writing of the zip into a in memory cache
        String archiveText = FileUtils.readFileToString(zipArchive);
        FileUtils.writeStringToFile(new File(resultZipFile), archiveText);

        //resultingArchive.zip exists, contains a compressed data.txt, but it can not
        //be read using winrar: CRC failed in data.txt. The file is corrupt

    }

}
  • 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-15T03:49:53+00:00Added an answer on May 15, 2026 at 3:49 am

    server send a string representation of
    data.zip to the repository

    So you want to get a string (i.e. textual) representation of a zip (i.e. binary) stream.

    Base64 is the most popular way to do this.

    One popular Java implementation is from Apache commons (codec component)

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

Sidebar

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.