Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Zipping a file

Posted on 2003-11-26
10
Medium Priority
?
260 Views
Last Modified: 2010-05-18
I have two strings. Each of which has to become a separate file in one zip file.  I have NO IDEA how to turn these strings into files without actually writing them to a harddrive. Can someone please show me how I would do this???

Thanks!

Tom
0
Comment
Question by:LPTech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9825547
I *think* you have to write them to the harddrive...

hopefully, someone can prove me wrong...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9825561
See

http://javaalmanac.com/egs/java.util.zip/CreateZip.html

Use a ByteArrayInputStream on the String instead of a file stream
0
 

Author Comment

by:LPTech
ID: 9825700
So, if my two strings were: policyExport and billExport

This is how I would do it?

-----------------------------

// These are the files to include in the ZIP file
    String[] filenames = new String[]{"policyExport", "billExport"};
   
    // 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++) {
            ByteArrayInputStream in = new ByteArrayInputStream(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) {
    }

----------------------

Then I could just push the one zip file out to the browser normally??

Thanks!

Tom
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 35

Expert Comment

by:TimYates
ID: 9825727
Cool :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9825735
Clarification please: do you want

a. the Strings to be entry names
b. the String to be content

if a., what is the content?
if b., what are the entry names?
0
 

Author Comment

by:LPTech
ID: 9825801
The strings are the content.

The entry names should be:

STRING          ENTRY NAME
--------------------------------
policyExport    WS93J2II.L0003757
billExport        WS93J2II.L0545757


And the zip file name should be, let's say, exports.zip

Thanks!

Tom
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1300 total points
ID: 9825856
import java.io.*;
import java.util.zip.*;


public class ZipStrings
{

    public void zipEm() {

      // These are the files to include in the ZIP file

      String[] filenames = new String[]{"WS93J2II.L0003757", "WS93J2II.L0545757"};

      String[] toZip = new String[]{"policyExport", "billExport"};

      // 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++) {
              ByteArrayInputStream in = new ByteArrayInputStream(toZip[i].getBytes());

              // 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) {
      }
    }

    public static void main(String[] args) {
      new ZipStrings().zipEm();
    }

}
0
 

Author Comment

by:LPTech
ID: 9825914
Thank you!
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9825915
> Then I could just push the one zip file out to the browser normally??

If you are using a servlet to push this file straight out to a browser, you can use CEHJ's code, just change:

          ZipOutputStream out = new ZipOutputStream( response.getOutputStream() ) ;

and add:

          response.setContentType( "application/unknow" );
          response.setHeader( "Content-Disposition", "attachment; filename=" + outFilename ) ;

before you start zipping :-)

Tim
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9825946
:-)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question