• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

writing data to zip file

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

i am trying to write data to zip file reading from another file

using this code.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZippingFileExample {
public static void main(String[] args) {
try {
String source = "dummy-1.txt";
String target = "data-1.zip";

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(target));
FileInputStream fis = new FileInputStream(source);

// put a new ZipEntry in the ZipOutputStream
zos.putNextEntry(new ZipEntry(source));

int size = 0;
byte[] buffer = new byte[1024];

// read data to the end of the source file and write it to the zip
// output stream.
while ((size = fis.read(buffer, 0, buffer.length)) > 0) {
zos.write(buffer, 0, size);
}

zos.closeEntry();
fis.close();

// Finish zip process
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


how to write data to zip file reading from input .
without storing data in temp file.

Thanks & regards
Ranjith
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused - is there a temp file created somewhere? I see two files being used - the source file and the destination file; is that what you intended to achieve? If not, what, exactly, are you trying to do, and how does the code you have so far deviate from that?
 
gendhe ranjith kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

the above example shows the writing data to zip file reading from dummy-1.txt

how to write data to zip file reading from input like(keyboard).
i saw some example in that they stored data in one temp file.

after that they will copy the data to zip file using copy stream logic.

is there any way to write data from input devices to zip file.

Thanks & Regards
Ranjith.

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of a FileInputStream you can use any other kind of InputStream, including ByteArrayInputStream and StringBufferInputStream if the data is in memory already.
 
reply
    Bookmark Topic Watch Topic
  • New Topic