Question

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();
        }

    }
}
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top