0

I would like to create zip file. File will contains exported Preferences and Serializable object. But when i try replace Object in zip archive saved Preferences disapear. How solve this problem?

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
import java.util.prefs.Preferences;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ToZip {

    /**
     * @param args
     */
    static Preferences exportPrefs = Preferences
            .userNodeForPackage(ToZip.class);

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        exportPrefs.put("Val", "MyVal");

        writeFile();
        readFile();
        System.out.println("Value:" + exportPrefs.get("Val", ""));

        // And Try replace object
        writeObject();
        readObject();
    }

    private static void writeFile() {

        String str = "ABCD";

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("/home/user/profile.prof");
            BufferedOutputStream boS = new BufferedOutputStream(fos);
            ZipOutputStream zoS = new ZipOutputStream(boS);
            ObjectOutputStream ooS = null;

            zoS.setMethod(ZipOutputStream.DEFLATED);
            zoS.setLevel(Deflater.BEST_COMPRESSION);

            zoS.putNextEntry(new ZipEntry("Object"));

            ooS = new ObjectOutputStream(zoS);
            ooS.writeObject(str);

            zoS.putNextEntry(new ZipEntry("Profile"));
            exportPrefs.exportSubtree(zoS);

            try {
                ooS.close();
            } catch (NullPointerException ex) {
            }

            zoS.close();
            fos.close();
            boS.close();
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (BackingStoreException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }

    private static void readFile() {

        FileInputStream fiN;
        try {
            fiN = new FileInputStream("/home/user/profile.prof");
            ZipInputStream ziS = new ZipInputStream(fiN);

            ziS.getNextEntry();

            ObjectInputStream oiS = new ObjectInputStream(ziS);
            System.out.println("Read String " + oiS.readObject());

            ziS.getNextEntry();

            exportPrefs.importPreferences(ziS);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidPreferencesFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static void writeObject() {// replace serialize object

        String str2 = "XYZ";

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("/home/user/profile.prof");
            BufferedOutputStream boS = new BufferedOutputStream(fos);
            ZipOutputStream zoS = new ZipOutputStream(boS);
            ObjectOutputStream ooS = null;

            zoS.setMethod(ZipOutputStream.DEFLATED);
            zoS.setLevel(Deflater.BEST_COMPRESSION);

            zoS.putNextEntry(new ZipEntry("Object"));

            ooS = new ObjectOutputStream(zoS);
            ooS.writeObject(str2);

            try {
                ooS.close();
            } catch (NullPointerException ex) {
            }

            zoS.close();
            fos.close();
            boS.close();
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }

    private static void readObject() {

        FileInputStream fiN;
        try {
            fiN = new FileInputStream("/home/user/profile.prof");
            ZipInputStream ziS = new ZipInputStream(fiN);
            ziS.getNextEntry();

            ObjectInputStream oiS = new ObjectInputStream(ziS);
            System.out.println("Read String " + oiS.readObject());

            oiS.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
user902691
  • 1,149
  • 4
  • 20
  • 46

1 Answers1

1

You don't seem to be exporting the preferences in your writeObject() nor reading them in your readObject() methods.

In writeObject(), you're missing:

        exportPrefs.exportSubtree(zoS);

And you're not reading them in your readObject()

        exportPrefs.importPreferences(ziS);
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • You don't understand my. I would like only change "Object", and don't change other file in zip archive. – user902691 Jul 23 '12 at 20:13
  • @user902691 You are calling `writeFile()` which writes an object to a zip file, then the preferences, then calling `readFile()`, which reads an object from a zip file, then reading the preferences. Then you are calling `writeObject()` which writes (by overwriting the existing file) another object to a zip but not the preferences. This is why you are missing the preferences. – Jon Lin Jul 23 '12 at 20:17
  • Ok. But how append file into archive don't loss other file into zip? – user902691 Jul 23 '12 at 20:48
  • @user902691 see my answer, you are missing 2 lines which write and read the prefs. This is why, as you asked in your question, when you **try replace Object in zip archive saved Preferences disapear.** – Jon Lin Jul 23 '12 at 20:50
  • Ok i understand. But how create "new archive" without rewriting Preferences? It is possible? – user902691 Jul 23 '12 at 20:53
  • @user902691 You are missing 2 lines from your code, in those 2 methods, this is why the prefs are missing, because you *are not writing them to your zipfile* – Jon Lin Jul 23 '12 at 20:55
  • I don't believe you can "update" a zip file. The only thing you can do is read the entries from the original file you want to keep and write them to a new file, along with the new content. Delete the old file and rename the new file back in it's place. This is how zip works – MadProgrammer Jul 23 '12 at 21:17
  • Thanks for help. I think that some simpler methods exits. – user902691 Jul 23 '12 at 21:24