Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1.    public static void zipFile(Map<String, Employee> employees,String filename){
  2.         try {
  3.             FileOutputStream fos = new FileOutputStream(filename);
  4.             ZipOutputStream zos= new ZipOutputStream(fos);
  5.  
  6.             zos.putNextEntry(new ZipEntry("file"));
  7.             ObjectOutputStream oos=new ObjectOutputStream(zos);
  8.             oos.writeObject(employees);
  9.             zos.closeEntry();
  10.             zos.close();
  11.         } catch(FileNotFoundException ex) {
  12.             System.err.format("Nie znaleziono pliku !");
  13.         }catch(IOException ex){
  14.             System.err.println("I/O error: " +ex);
  15.         }
  16.     }
  17.  
  18.     public static void gzipFile(Map<String, Employee> employees,String filename){
  19.        try {
  20.            FileOutputStream fos = new FileOutputStream(filename);
  21.            GZIPOutputStream gz = new GZIPOutputStream(fos);
  22.            ObjectOutputStream oos=new ObjectOutputStream(gz);
  23.            oos.writeObject(employees);
  24.            gz.close();
  25.            fos.close();
  26.        } catch(FileNotFoundException ex) {
  27.            System.err.format("Nie znaleziono pliku !");
  28.        }catch(IOException ex){
  29.            System.err.println("I/O error: " +ex);
  30.        }
  31.     }
  32.  
  33. //czytanie z pliku 
  34.  
  35.     public static Map<String,Employee>loadFiles(String filename){
  36.  
  37.         if(filename.endsWith(".zip")){
  38.             try {
  39.                 FileInputStream fis = new FileInputStream(filename);
  40.                 ZipInputStream zis = new ZipInputStream(fis);
  41.                 ZipEntry ze = zis.getNextEntry();
  42.                 ObjectInputStream ois = new ObjectInputStream(zis);
  43.                 Map<String, Employee> temp =(Map<String, Employee>) ois.readObject();
  44.                 fis.close();
  45.                 return temp;
  46.             }catch(FileNotFoundException ex){
  47.                 System.err.format("Nie znaleziono pliku !");
  48.                 return null;
  49.             }catch(IOException ex){
  50.                 System.err.println("I/O error: " +ex);
  51.                 return null;
  52.             }catch(ClassNotFoundException ex){
  53.                 System.err.println("Nie znaleziono klasy " +ex);
  54.                 return null;
  55.             }
  56.         }
  57.         else if(filename.endsWith(".gzip")){
  58.             try{
  59.             FileInputStream fis = new FileInputStream(filename);
  60.             GZIPInputStream gz = new GZIPInputStream(fis);
  61.             ObjectInputStream ois=new ObjectInputStream(gz);
  62.             Map<String,Employee>temp= (Map<String, Employee>) ois.readObject();
  63.             fis.close();
  64.             return temp;
  65.             }catch(FileNotFoundException ex){
  66.                 System.err.format("Nie znaleziono pliku !");
  67.                 return null;
  68.             }catch(IOException ex){
  69.                 System.err.println("I/O error: " +ex);
  70.                 return null;
  71.             }catch(ClassNotFoundException ex){
  72.                 System.err.println("Nie znaleziono klasy " +ex);
  73.                 return null;
  74.             }
  75.         }
  76.         return null;
  77.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement