Solved

Zipping a file

Posted on 2003-11-26
10
254 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 325 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Groovy problem when using SOAPUI : DispatchException occurred 7 46
MySQL  on Tomcat 8 47
Desingning Refactoring existing code 2 24
What's wrong with this code? 4 23
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

831 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