How to zip text files ?

Hello to everybody,
My problems is to compress text files into a .zip file, I have used the following method:
public void zippa() {
FileReader fileIn;
BufferedReader in;
String riga;
boolean eof = false;
ZipEntry zEntry;
try {
FileOutputStream fout = new FileOutputStream("C:\\Davide\\zip\\W4.zip");
fileIn = new FileReader("C:\\Davide\\zip\\W4.txt");
in = new BufferedReader(fileIn);
riga = in.readLine();
FileWriter fileOut = null;
BufferedWriter out = null;
CheckedOutputStream csum = new CheckedOutputStream(fout, new Adler32());
ZipOutputStream z = new ZipOutputStream(new BufferedOutputStream(csum));
zEntry = new ZipEntry("C:\\Davide\\zip\\Sonia.txt");
z.putNextEntry(new ZipEntry(zEntry));
fileIn = new FileReader("C:\\Davide\\zip\\W4.txt");
in = new BufferedReader(fileIn);
riga = in.readLine();
System.out.println("Zipping....");
while (!eof) {
riga = in.readLine();
     if (riga != null) {
z.write(riga.getBytes(),0,riga.length());
} else eof = true;
System.out.println("Zipped....");
z.close();
fileIn.close();
} catch (IOException ioe) {
System.out.println("Eccezione: "+ioe);
ioe.printStackTrace();
My problem consist in the fact that the different lines which are coming from W4.txt, are not separeted (in the zip entry "Sonia.txt") by a carriage return (new line) character but by an unprintable character (which looks like a square).... I've tried to add a "\n" string to the String variable riga, or to add the following lines:
z.write((char)13);
z.write((char)12);
immediately after the
z.write(riga.getBytes(),0,riga.length());
instruction, but these solutions does not work.
Is someone so kind to help me please ?
Vacca Davide
[email protected]

The readLine() method removes new line characters as it returns one line of data at a time. When creating a zip file you want to retain all the characters in the input file. Below is the code I used to test the creation and reading of a zip file. Hope it helps.
import java.io.*;
import java.util.zip.*;
public class TestZip
public static void main(String[] args) throws IOException
zip( args[0] );
System.exit(0);
static private void zip(String fileName) throws IOException
          // Write Zip file
byte[] iobuff = new byte[512];
int bytes;
FileInputStream fis = new FileInputStream( fileName );
ZipOutputStream zos = new ZipOutputStream( new FileOutputStream( fileName + ".ZIP" ) );
ZipEntry ze = new ZipEntry( fileName );
zos.putNextEntry( ze );
while ( (bytes = fis.read( iobuff )) != -1 )
zos.write( iobuff, 0, bytes );
fis.close();
zos.close();
          // Read Zip file and rewrite to verify contents are the same
ZipInputStream zis = new ZipInputStream( new FileInputStream( fileName + ".ZIP" ) );
zis.getNextEntry();
FileOutputStream fos = new FileOutputStream (fileName + ".UNZIP" );
while ( (bytes = zis.read( iobuff, 0, 512 )) != -1 )
fos.write( iobuff, 0, bytes );
fos.close();
zis.close();

Similar Messages

  • How to zip multi files into one zip file in ABAP

    hi all:
    As you know, ABAP has CL_ABAP_GZIP class to support zip file function. But it only support one file zip. If I want to zip two files into one zip file, how to do it?
    thanks.

    Hi,
    <li>If you know how to zip one file,  to zip two files , you can follow the below steps.
    DATA:g_zipper     TYPE REF TO cl_abap_zip.
    "create our zipper object
    CREATE OBJECT g_zipper.
    "add 1st file to zip
    CALL METHOD g_zipper->add
       EXPORTING
         name    = file_name
         content = content_x.
      "add 2nd file to zip
    CALL METHOD g_zipper->add
       EXPORTING
         name    = file_name
         content = content_y.
    "save zip
    CALL METHOD g_zipper->save
       RECEIVING
         zip = zip.
    Thanks
    Venkat.O

  • How to Zip Excel files using File Adapter?

    Hi,
    We have tried to ZIP the Excel file with  PayloadZipBean in File adapter. But we faced some issue while zipping.
    We have seen some zunk data in excel file after zipping with PayloadZipBean. Someone please help how to zip Excel files in PI with File Adapter.
    Regards,
    Sreeramulu Konjeti.

    Hi Sree,
    If you are facing any issue with PayloadZipBean then you can use java mapping to Zip the files.
    Please find the complete Java mapping code to zipt the file
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/50ce0433-4309-2b10-4bb4-d421e78463f7?quicklink=index&overridelayout=true

  • How to zip one file i have on disk with java.util.zip

    I don't know how to zip a file. I managed to do new html file, but i don't know how to zip it.
    This is my code:
    PrintWriter pw = new PrintWriter (new FileWriter(export), true);
    Thanks!

    heres a zipper class i wrote which i have butchered a bit to make more generic (its basic and at present only takes one zipentry etc, but very simple and should be easy to extend etc) Just pass it your file in its constructor and then call its zipme method. PS if it can be made better let me know so i can update my class, cheers.
    import java.io.*;
    import java.util.zip.*;
    import java.awt.*;
    class zipper
         public File file;
         public zipper(File f)
              file = new File(f);
         public void zipme()
              try
                   FileInputStream fin = new FileInputStream(file);
                   int a = (int)file.length();
                   byte b[] = new byte[a];
                   fin.read(b);
                   ZipEntry k = new ZipEntry(fin.getName());//represents a single file in a zip archive!
                   File newFile = new File("D:\\AZipFile.zip");
                   ZipOutputStream zi = new ZipOutputStream(new FileOutputStream(newFile));
                   zi.putNextEntry(k);
                   zi.write(b,0,a);
                   zi.close();
                   fin.close();
              catch(FileNotFoundException e)
                   //System.out.println("ERROR File not found");
              catch(IOException ee)
                   //System.out.println("ERROR IO exception in Zipping file");
    }

  • How to write text file in Shockwave?

    Does anybody know how to write text file in Shockwave to
    user's disk?
    Thanks in advance.

    Those Xtras can wreak to much havoc when used with the wrong
    intent.
    What you can do is write with setpref and store a list of
    saves and the
    saves itself seperatly. Then you'd have to build your own
    save/open
    dialog to let the user:
    * pick a previously saved file to load or overwrite
    * have the user type the name of a new file to save.
    Only thing that remains is that the user cannot decide where
    the files
    are saved.
    Manno
    SiuLinda wrote:
    > Thanks a lot for your reply.
    > Yes, cookies is good but I have to write a program to
    save the text file in
    > where the user wants, user can open these files later if
    they like, like using
    > Filextra and Fileio, but I found all of these xtras seem
    to be not supported in
    > shockwave.
    >
    Manno Bult
    [email protected]

  • How to ZIP the files and folders/sub folder files using java

    HI All,
    I'm New to this Forum, Could anybody tell me how to zip the files and folders/sub folders using java. For example I have a folder with the name testfolder and side that folder I have some files and some sub folders inside subfolders I have some other files. I need to ZIP the files and folders as they are in same hierarchy.
    Any pointers or help wolud be appritiated.
    Thanks,
    Rajeshbabu V

    [http://www.devx.com/tips/Tip/14049]

  • How to read text file line by line...?

    how to read text file line by line, but the linefeed is defined by user, return list of string, each line of file is a item of list?
    please help me.
    Thanks very much

    Brynjar wrote:
    In Groovy, you would do something like:
    linefeed = "\n" //or "\r\n" if the user chose so
    lines = new File('pathtofile').text.split("${linefeed}")This is one of the things that has always annoyed me about Sun's sdk, i.e. the lack of easy ways to do things like that. You always end up making your own utilities or use something like Apache's commons.io. Same goes for jdbc and xml - I'll wait for appropriate topics to show how easy that is in Groovy :)I generally agree, but what I really don't like about the Groovy text-file handling niceties: They don't care about encoding/always use the default encoding. And as soon as you want to specify the encoding, it gets a lot more complex (granted, it's still easier than in Java).

  • How to load text file data to Oracle Database table?

    By using Oracle Forms, how to load text file data to Oracle Database table?

    Metalink note 33247.1 explains how to use text_io as suggested by Robin to read the file into a Multi-Row block. However, that article was written for forms 4.5 and uses CREATE_RECORD in a loop. There was another article, 91513.1 describing the more elegant method of 'querying' the file into the block by transactional triggers. Unfortunately this more recent article has disappeared without trace and Oracle deny its existence. I know it existed as I have a printed copy in front of me, and very useful it is too.

  • How to zip a file on mac pro?

    how to zip a file on mac pro?

    Tristan,
    Select multiple items that you want to zip, right click and choose "Compress XX Items."

  • How to load text files in GUI

    plz tell me .. how to load and compare two text files using file popup's . example file i have attached..
    Attachments:
    testW_FF.txt ‏2 KB

    I don't understand whether your question is on how to load text files or how to show them on a panel or how to compare them... or all aspects together!
    The first operation (loading the file) can be accomplished with functions included in the Formatting and I/O Library like OpenFile, ReadFile and so on; with a file like yours even FileToArray could be an option.
    How to show the data on screen is heavily dependent on what you intend to do with them: data can be shown in textboxes, listboxes, tables or graphs so... what do you want to do?
    The same applies with comparison: without additional details is difficult to give you the proper hint.
    Proud to use LW/CVI from 3.1 on.
    My contributions to the Developer Zone Community
    If I have helped you, why not giving me a kudos?

  • Step by Step"How JSP read text file :

    Hi ,
    Any one know or have a good site to show step by step how JSP read text file.
    TQ.

    There is no difference Between reading a text file from JSP and reading a text file from Java.
    Just follow the same steps for JSP also.

  • How to call text file using Script in Data Integrator

    Dear All,
    Can any one assit me in how to call a text file using script with the help of Data Integrator.
    and one question ?
    M having 32 csv files i want to club thos 32 csv files into one table with the help of Data Integrator, can
    any one assist me.

    mary,
    since you knew the file name ,when clicked in name send to server,read the file and write to servlet outputstream.
    I think this would help you.
    If anything wrong in mycode ..forums will help you further
    BufferedInputStream bis=null;
    BufferedOutputStream bos=null;
    int bytesRead=0;
    byte buff[]=new byte[1024];
    File f=new File(test.txt);
    try{
         bis= new BufferedInputStream(new FileInputStream(f));
         bytesRead=bis.read(buff,0,buff.length);
         if(bytesRead!=-1){
              // create a BufferedOutputStream from ServletOutputStream
              bos=new BufferedInputStream(response.getOutputStream());
              do{
                   bos.write(buff,0,bytesRead);
              }while((bytesRead=bis.read(buff,0,buff.length))!=-1)
    }catch(Exception e){
         ////error handling
         }

  • How to Zip server files(in a directory) and throwing back to client

    Can someone tell me how to zip all the files in a particular directory of my web application and throwing back to client to give option for save that zipped file.
    i have used the below used function to zip in servlet. But I cann't zip a particular directory of my application in server.
    Please help
    private void zipDir(String dir2zip, ZipOutputStream zos)
    try
    //create a new File object based on the directory we
    // have to zip File
    File fileDir2Zip = new File(dir2zip);
    System.out.println("is directory ..."+fileDir2Zip.isDirectory());
    //get a listing of the directory content
    String[] dirList = fileDir2Zip.list();
    for(int i = 0 ; i < dirList.length ; i++)
    System.out.println("Dir list ..."+dirList);
    byte[] readBuffer = new byte[2156];
    int bytesIn = 0;
    //loop through dirList, and zip the files
    for(int i=0; i<dirList.length; i++)
    File f = new File(fileDir2Zip, dirList[i]);
    if(f.isDirectory())
    //if the File object is a directory, call this
    //function again to add its content recursively
    String filePath = f.getPath();
    zipDir(filePath, zos);
    //loop again
    continue;
    //if we reached here, the File object f was not
    //a directory
    //create a FileInputStream on top of f
    FileInputStream fis = new FileInputStream(f);
    //create a new zip entry
    ZipEntry anEntry = new ZipEntry(f.getPath());
    //place the zip entry in the ZipOutputStream object
    zos.putNextEntry(anEntry);
    //now write the content of the file to the ZipOutputStream
    while((bytesIn = fis.read(readBuffer)) != -1)
    zos.write(readBuffer, 0, bytesIn);
    System.out.println("Last");
    //close the Stream
    fis.close();
    catch(Exception e)
    e.printStackTrace();

    Hi
    Whatever file exist in directory should execute with out any condition because they delete all files after exection of my process chain. They are using another process chain once a day for deletion of all files in that directory.
    So directory always contains fresh files.
    Here by using routine in infopackge i am unable to run all files one by one at a time.
    As of my knowledge  by using infopackage we can run only one file at a time.
    Here my question is , can we run more than one file in single run of infopackage?
    or can we have any function module to run the infopackage from externally? so that i can use function module in abap program? any ideas..
    Regards
    Raju

  • How to read text file content in portal application?

    Hi,
    How do we read text file content in portal application?
    Can anyone forward the code to do do?
    Regards,
    Anagha

    Check the code below. This help you to know how to read the text file content line by line. You can display as you require.
    IUser user = WPUMFactory.getServiceUserFactory().getServiceUser("cmadmin_service");
    IResourceContext resourceContext = new ResourceContext(user);
    String filePath = "/documents/....";
    RID rid = RID.getRID(filePath);
    IResource resource = ResourceFactory.getInstance().getResource(rid,resourceContext);
    InputStream inputStream = resource.getContent().getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String line = reader.readLine();
    while(line!=null) {
          line = reader.readLine();
         //You can append in string buffer to get file content as string object//
    Regards,
    Yoga

  • How to include text file in classes directory of war file.

    I have a project that uses a text file called cache.ccf (JCS configuration file) when debugging the webservices, I have to dig into the inflated war file directory and manually place the cache.ccf file in the WEB-INF/classes/ directory/
    How can i have JDeveloper wrap the file up in the war file in the WEB-INF/classes directory. both for debugging and when the war file is actually built for deployment.
    Thanks,
    Bart

    Nevermind, I figured it out using the Project Properties->Deployment and include it in the WEB-INF/classes

Maybe you are looking for

  • Computer can no longer find my printers unless I go to wireless

    I have an older 24" imac running 10.6.8 and am suddenly unable to print on any of my printers unless I go through wireless.If I go to print utility I get a communications error message but it's not the cable or printer. The computer says "printing" a

  • Zen Micro Photo bugs need to be worked out - firmware update comi

    Hi, I've had my Zen Micro Photo 8gb for a month & have discovered some interesting quirks: first, I don't believe mine is defecti've, though I've had problems. I have encountered freezing on both the "Zen Microphoto" logo and "rebuilding library" scr

  • Asking only once for a parameter...

    Post Author: Nightcrawler CA Forum: Data Connectivity and SQL Hi, I have a report which need to select a list of items for a dynamic lookup for the report. I have created two stored procedure in SQL which work in QA. When I link them to a report I ge

  • Two colums in F4 help

    Hi Folks, i want to restirct the hit list in selection screen( F4 hlp). i am using the FM F4IF_INT_TABLE_VALUE_REQUEST. my requirment is i want display the filed Cred.rep.grp(KNKK-SBGRP) along with another field Cred.contr.area(KNKK-KKBER) in the sam

  • Managing warranties for equipment components

    Hi! I have the following issue: I need to implement a solution where user can manage warranties for equipment's components besides the equipment warranty. User requires that when a new component is assigned to an equipment, like bill of materials or