Revision 37599 branches/v2_0_0_prep/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/utils/Compress.java

View differences:

Compress.java
46 46
 */
47 47
public class Compress {
48 48

  
49
//    private static final int BUFFER = 2048;
49
	// private static final int BUFFER = 2048;
50 50

  
51
    public void compressPluginAsPackageSet(File file, String fileName,
52
        OutputStream os) throws MakePluginPackageServiceException {
53
        List<File> files = new ArrayList<File>();
54
        List<String> fileNames = new ArrayList<String>();
55
        files.add(file);
56
        fileNames.add(fileName);
57
        compressPluginsAsPackageSet(files, fileNames, os);
58
    }
51
	public void compressPluginAsPackageSet(File file, String fileName,
52
			OutputStream os) throws MakePluginPackageServiceException {
53
		List<File> files = new ArrayList<File>();
54
		List<String> fileNames = new ArrayList<String>();
55
		files.add(file);
56
		fileNames.add(fileName);
57
		compressPluginsAsPackageSet(files, fileNames, os);
58
	}
59 59

  
60
    public void compressPluginsAsPackageSet(File directory, OutputStream os)
61
        throws MakePluginPackageServiceException {
62
        File[] files = directory.listFiles();
63
        List<File> filesArray = new ArrayList<File>();
64
        List<String> fileNamesArray = new ArrayList<String>();
65
        for (int i = 0; i < files.length; i++) {
66
            filesArray.add(files[i]);
67
            fileNamesArray.add(files[i].getName());
68
        }
69
        compressPluginsAsPackageSet(filesArray, fileNamesArray, os);
70
    }
60
	public void compressPluginsAsPackageSet(File directory, OutputStream os)
61
			throws MakePluginPackageServiceException {
62
		File[] files = directory.listFiles();
63
		List<File> filesArray = new ArrayList<File>();
64
		List<String> fileNamesArray = new ArrayList<String>();
65
		for (int i = 0; i < files.length; i++) {
66
			filesArray.add(files[i]);
67
			fileNamesArray.add(files[i].getName());
68
		}
69
		compressPluginsAsPackageSet(filesArray, fileNamesArray, os);
70
	}
71 71

  
72
    public void compressPluginsAsPackageSet(List<File> files,
73
        List<String> fileNames, OutputStream os)
74
        throws MakePluginPackageServiceException {
75
        try {
76
            ZipOutputStream zos =
77
                new ZipOutputStream(new BufferedOutputStream(os));
72
	public void compressPluginsAsPackageSet(List<File> files,
73
			List<String> fileNames, OutputStream os)
74
			throws MakePluginPackageServiceException {
75
		try {
76
			ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(
77
					os));
78 78

  
79
            for (int i = 0; i < files.size(); i++) {
80
                ZipEntry zipEntry = new ZipEntry(fileNames.get(i));
81
                zos.putNextEntry(zipEntry);
82
                compressPluginFiles(files.get(i), zos);
83
                zos.closeEntry();
84
            }
85
            zos.close();
86
        } catch (Exception e) {
87
            throw new MakePluginPackageServiceException(
88
                "Error compressing as package set the plugin files: " + files,
89
                e);
90
        }
91
    }
79
			for (int i = 0; i < files.size(); i++) {
80
				ZipEntry zipEntry = new ZipEntry(fileNames.get(i));
81
				zos.putNextEntry(zipEntry);
82
				compressPluginFiles(files.get(i), zos);
83
				zos.closeEntry();
84
			}
85
			zos.close();
86
		} catch (Exception e) {
87
			throw new MakePluginPackageServiceException(
88
					"Error compressing as package set the plugin files: "
89
							+ files, e);
90
		}
91
	}
92 92

  
93
    public void compressPluginAsPackage(File folder, OutputStream os)
94
        throws MakePluginPackageServiceException {
95
        ZipOutputStream zos = new ZipOutputStream(os);
96
        try {
97
            int parentFileLenght = folder.getParentFile().toString().length();
98
            compressPluginFile(folder, parentFileLenght, zos);
99
            zos.flush();
100
            zos.close();
101
        } catch (IOException e) {
102
            throw new MakePluginPackageServiceException(
103
                "Error compressing as package the plugin folder: " + folder, e);
104
        }
105
    }
93
	public void compressPluginAsPackage(File folder, OutputStream os)
94
			throws MakePluginPackageServiceException {
95
		ZipOutputStream zos = new ZipOutputStream(os);
96
		try {
97
			int parentFileLenght = folder.getParentFile().toString().length();
98
			compressPluginFile(folder, parentFileLenght, zos);
99
			zos.flush();
100
			zos.close();
101
		} catch (IOException e) {
102
			throw new MakePluginPackageServiceException(
103
					"Error compressing as package the plugin folder: " + folder,
104
					e);
105
		}
106
	}
106 107

  
107
    public void compressPluginAsPackageIndex(File folder, OutputStream os)
108
        throws MakePluginPackageServiceException {
109
        ZipOutputStream zos = new ZipOutputStream(os);
110
        try {
111
            int parentFileLength = folder.getParentFile().toString().length();
112
            String folderName =
113
                folder.toString()
114
                    .substring(parentFileLength + 1, folder.toString().length());
115
            
116
            File infoFile =
117
                new File(folder, getPackageInfoFileName() + ".index");
118
            if( !infoFile.exists() ) {
119
                infoFile = new File(folder, getPackageInfoFileName());
120
            }
108
	public void compressPluginAsPackageIndex(File folder, OutputStream os)
109
			throws MakePluginPackageServiceException {
110
		ZipOutputStream zos = new ZipOutputStream(os);
111
		try {
112
			int parentFileLength = folder.getParentFile().toString().length();
113
			String folderName = folder.toString().substring(
114
					parentFileLength + 1, folder.toString().length());
121 115

  
122
            byte[] buf = new byte[1024];
123
            int len;
116
			File infoFile = new File(folder, getPackageInfoFileName()
117
					+ ".index");
118
			if (!infoFile.exists()) {
119
				infoFile = new File(folder, getPackageInfoFileName());
120
			}
124 121

  
125
            ZipEntry zipEntry =
126
                new ZipEntry(folderName + File.separator
127
                    + getPackageInfoFileName());
128
            zos.putNextEntry(zipEntry);
122
			byte[] buf = new byte[1024];
123
			int len;
129 124

  
130
            FileInputStream fin = new FileInputStream(infoFile);
131
            BufferedInputStream in = new BufferedInputStream(fin);
132
            while ((len = in.read(buf)) >= 0) {
133
                zos.write(buf, 0, len);
134
            }
135
            in.close();
136
            zos.closeEntry();
137
            zos.flush();
138
            zos.close();
139
        } catch (IOException e) {
140
            throw new MakePluginPackageServiceException(
141
                "Error compressing as package index the plugin folder: "
142
                    + folder, e);
143
        }
144
    }
125
			ZipEntry zipEntry = new ZipEntry(folderName + File.separator
126
					+ getPackageInfoFileName());
127
			zos.putNextEntry(zipEntry);
145 128

  
146
    private void compressPluginFiles(File fileOrFolder, ZipOutputStream zos)
147
        throws IOException {
148
        int parentFileLenght = fileOrFolder.getParentFile().toString().length();
149
        ZipOutputStream zosPlugin = new ZipOutputStream(zos);
150
        compressPluginFile(fileOrFolder, parentFileLenght, zosPlugin);
151
        zosPlugin.finish();
152
    }
129
			FileInputStream fin = new FileInputStream(infoFile);
130
			BufferedInputStream in = new BufferedInputStream(fin);
131
			while ((len = in.read(buf)) >= 0) {
132
				zos.write(buf, 0, len);
133
			}
134
			in.close();
135
			zos.closeEntry();
136
			zos.flush();
137
			zos.close();
138
		} catch (IOException e) {
139
			throw new MakePluginPackageServiceException(
140
					"Error compressing as package index the plugin folder: "
141
							+ folder, e);
142
		}
143
	}
153 144

  
154
    private void compressPluginFile(File file, int parenFileLength,
155
        ZipOutputStream zosPlugin) throws IOException {
156
        String fileName =
157
            file.toString()
158
                .substring(parenFileLength, file.toString().length());
159
        if (File.separatorChar != '/') {
160
            fileName = fileName.replace(File.separatorChar, '/');
161
        }
145
	private void compressPluginFiles(File fileOrFolder, ZipOutputStream zos)
146
			throws IOException {
147
		int parentFileLenght = fileOrFolder.getParentFile().toString().length();
148
		ZipOutputStream zosPlugin = new ZipOutputStream(zos);
149
		compressPluginFile(fileOrFolder, parentFileLenght, zosPlugin);
150
		zosPlugin.finish();
151
	}
162 152

  
163
        if (file.isDirectory()) {
164
            // Remove the subversion folders
165
            if (!file.getName().toUpperCase().equals(".SVN")) {
166
                // Adding the files
167
                String[] fileNames = file.list();
168
                if (fileNames != null) {
169
                    for (int i = 0; i < fileNames.length; i++) {
170
                        compressPluginFile(new File(file, fileNames[i]),
171
                            parenFileLength, zosPlugin);
172
                    }
173
                }
174
            }
175
        } else {
176
            byte[] buf = new byte[1024];
177
            int len;
153
	private void compressPluginFile(File file, int parenFileLength,
154
			ZipOutputStream zosPlugin) throws IOException {
155
		String fileName = file.toString().substring(parenFileLength,
156
				file.toString().length());
157
		if (File.separatorChar != '/') {
158
			fileName = fileName.replace(File.separatorChar, '/');
159
		}
178 160

  
179
            ZipEntry zipEntry = new ZipEntry(fileName);
180
            zosPlugin.putNextEntry(zipEntry);
161
		if (file.isDirectory()) {
162
			// Remove the subversion folders
163
			if (!file.getName().toUpperCase().equals(".SVN")) {
164
				// Adding the files
165
				String[] fileNames = file.list();
166
				if (fileNames != null) {
167
					for (int i = 0; i < fileNames.length; i++) {
168
						compressPluginFile(new File(file, fileNames[i]),
169
								parenFileLength, zosPlugin);
170
					}
171
				}
172
			}
173
		} else {
174
			byte[] buf = new byte[1024];
175
			int len;
181 176

  
182
            FileInputStream fin = new FileInputStream(file);
183
            BufferedInputStream in = new BufferedInputStream(fin);
184
            while ((len = in.read(buf)) >= 0) {
185
                zosPlugin.write(buf, 0, len);
186
            }
187
            in.close();
188
            zosPlugin.closeEntry();
189
        }
190
    }
177
			ZipEntry zipEntry = new ZipEntry(fileName);
178
			zosPlugin.putNextEntry(zipEntry);
191 179

  
192
    private String getPackageInfoFileName() {
193
        return InstallerProviderLocator.getProviderManager()
194
            .getPackageInfoFileName();
195
    }
180
			FileInputStream fin = new FileInputStream(file);
181
			BufferedInputStream in = new BufferedInputStream(fin);
182
			while ((len = in.read(buf)) >= 0) {
183
				zosPlugin.write(buf, 0, len);
184
			}
185
			in.close();
186
			zosPlugin.closeEntry();
187
		}
188
	}
189

  
190
	private String getPackageInfoFileName() {
191
		return InstallerProviderLocator.getProviderManager()
192
				.getPackageInfoFileName();
193
	}
196 194
}

Also available in: Unified diff