Solved

Zipping a file

Posted on 2003-11-26
10
258 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

738 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