Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Zipping a file

Posted on 2003-11-26
10
Medium Priority
?
261 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

783 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