Skip to main content
summaryrefslogtreecommitdiffstats
blob: 067defd8500d8e5cce1741944ff9c2bddc01e892 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.util.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import org.eclipse.osee.framework.jdk.core.util.Lib;

public class Zip {

   public static void zip(String[] filenames, String outFilename) {
      // These are the files to include in the ZIP file
      // String[] filenames = new String[]{"filename1", "filename2"};

      // Create a buffer for reading the files
      byte[] buf = new byte[1024];

      try {
         // Create the ZIP file
         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

         // Compress the files
         for (int i = 0; i < filenames.length; i++) {
            FileInputStream in = new FileInputStream(filenames[i]);

            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(filenames[i]));

            // Transfer bytes from the file to the ZIP file
            int len;
            while ((len = in.read(buf)) > 0) {
               out.write(buf, 0, len);
            }

            // Complete the entry
            out.closeEntry();
            in.close();
         }

         // Complete the ZIP file
         out.close();
      } catch (IOException e) {
         // do nothing
      }

   }

   public static void unzip(File zipFile, File destinationDir, IZipEntryCompleteCallback progressBar) throws IOException {
      int BUFFER = 2048;
      BufferedOutputStream dest = null;
      BufferedInputStream is = null;
      ZipEntry entry = null;

      try {
         ZipFile zipfile = new ZipFile(zipFile.getAbsolutePath());
         progressBar.setValue(0);
         progressBar.setMinimum(0);
         progressBar.setMaximum(zipfile.size());
         System.out.println(zipfile.size());
         Enumeration<? extends ZipEntry> e = zipfile.entries();
         int size = 0;
         while (e.hasMoreElements()) {
            entry = e.nextElement();
            is = new BufferedInputStream(zipfile.getInputStream(entry));
            int count;
            byte data[] = new byte[BUFFER];
            File fileDir = new File(destinationDir.getAbsolutePath() + File.separator + entry.getName());
            if (entry.isDirectory()) {
               fileDir.mkdirs();
               progressBar.setValue(++size);
               continue;
            } else {
               fileDir.getParentFile().mkdirs();
            }

            if (!fileDir.exists() || fileDir.exists() && fileDir.canWrite()) {
               FileOutputStream fos = new FileOutputStream(fileDir.getAbsolutePath());
               dest = new BufferedOutputStream(fos, BUFFER);
               while ((count = is.read(data, 0, BUFFER)) != -1) {
                  dest.write(data, 0, count);
               }
               dest.flush();
               dest.close();
            }

            is.close();

            if (fileDir.getAbsolutePath().endsWith(".lnk")) {
               if (fileDir.canWrite()) {
                  fileDir.setReadOnly();
               }
            }
            progressBar.setValue(++size);
         }
      } catch (RuntimeException ex) {
         String information =
            "ZipFile: " + (zipFile != null ? zipFile.getAbsolutePath() : "NULL") + "\n" + "DestinationDir: " + (destinationDir != null ? destinationDir.getAbsolutePath() : "NULL") + "\n" + "Entry Processed: " + (entry != null ? entry.toString() : "NULL") + "\n";
         Lib.close(is);
         throw new IOException(information + ex.getMessage());
      }
   }
}

Back to the top