CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 1999
    Posts
    3

    Writing to a zip file

    Hey friends,
    i want to write 3 array of shorts to a zip file using object serialisation. I tried doing it using zipoutputstream, but it says that the zip entry i am using is not ok. can anyone please send me a code which will enable me to read and write array of shorts to a ZIP file.
    Regards,
    Apurva


  2. #2
    Join Date
    Apr 1999
    Location
    Canada
    Posts
    12

    Re: Writing to a zip file

    Try this. It should work. Maybe you had problems with the putNextEntry/getNextEntry calls.

    [pre]
    import java.io.*;
    import java.util.zip.*;

    public class ZipShorts
    {
    public static void main (String[] args)
    {
    System.out.print ("Create the zip file ? <y/n> ");
    boolean create = false;
    try
    {
    String result = new BufferedReader (new InputStreamReader (System.in)).readLine ();
    create = result.toUpperCase ().startsWith ("Y");
    }
    catch (Exception e)
    {
    }
    if (create)
    {
    try
    {
    System.out.println ("Enter the arrays of shorts as lines of space-separated numbers.");
    System.out.println ("Finish with an empty line.");
    ZipOutputStream zos = new ZipOutputStream (new FileOutputStream ("Shorts.zip"));
    zos.putNextEntry (new ZipEntry ("Shorts"));
    ObjectOutputStream oos = new ObjectOutputStream (zos);
    try
    {
    String s = "";
    while (true)
    {
    s = new BufferedReader (new InputStreamReader (System.in)).readLine ();
    if (s.length () == 0)
    break;
    StreamTokenizer st = new StreamTokenizer (new StringReader (s));
    st.parseNumbers ();
    java.util.Vector vals = new java.util.Vector ();
    while (st.nextToken () != StreamTokenizer.TT_EOF)
    {
    if (st.ttype != StreamTokenizer.TT_NUMBER)
    {
    System.out.println ("Bad input: " + st.sval);
    System.exit (1);
    }
    vals.add (new Short ((short)st.nval));
    }
    short[] array = new short[vals.size ()];
    for (int i = 0;i < array.length;i++)
    array[i] = ((Short)vals.get (i)).shortValue ();
    oos.writeObject (array);
    }
    }
    catch (IOException ioe)
    {
    }
    zos.close ();
    }
    catch (Exception e)
    {
    e.printStackTrace ();
    }
    }
    else
    {
    try
    {
    ZipInputStream zis = new ZipInputStream (new FileInputStream ("Shorts.zip"));
    zis.getNextEntry ();
    ObjectInputStream ois = new ObjectInputStream (zis);
    try
    {
    for (int i = 1;;i++)
    {
    short[] array = (short[])ois.readObject ();
    String s = i + ") ";
    for (int j = 0;j < array.length;j++)
    {
    if (j > 0)
    s += ", ";
    s += array[j];
    }
    System.out.println (s);
    }
    }
    catch (IOException ioe)
    {
    }
    }
    catch (Exception e)
    {
    e.printStackTrace ();
    }
    }
    }
    }
    [/pre]


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured