Solved

Zipping a file

Posted on 2003-11-26
10
251 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
Comment Utility
I *think* you have to write them to the harddrive...

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

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Cool :-)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:LPTech
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you!
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> 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
Comment Utility
:-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now