Source code: com/pepperview/romzinger/ZippedGame.java
1 /*
2 * ZippedGame.java -
3 * Copyright (C) 2000 Fabrice Armisen
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.pepperview.romzinger;
21
22 import java.util.Hashtable;
23 import java.util.zip.ZipFile;
24 import java.util.zip.ZipEntry;
25 import java.util.zip.ZipInputStream;
26 import java.util.zip.ZipOutputStream;
27 import java.util.Enumeration;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32
33 /**
34 * Description of the Class
35 *
36 * @author fabrice
37 * @created May 15, 2001
38 */
39 public class ZippedGame extends Game
40 {
41 private File file = null;
42 private Hashtable crcTable = null;
43
44
45 /**
46 * Constructor for the ZippedGame object
47 *
48 * @since
49 */
50 public ZippedGame()
51 {
52 crcTable = new Hashtable();
53 }
54
55
56 /**
57 * Sets the name of a ROM of this ZippedGame object
58 *
59 * @param rom the rom
60 * @param name The new name value
61 * @since
62 */
63 public void setROMName( ROMDesc rom, String name )
64 {
65 rom.setName( name );
66 }
67
68
69 /**
70 * Gets the File object corresponding to this game
71 *
72 * @return The File value
73 * @since
74 */
75 public File getFile()
76 {
77 return file;
78 }
79
80
81 /**
82 * Gets the Name this game
83 *
84 * @return The Name value
85 * @since
86 */
87 public String getName()
88 {
89 return file.getName().substring( 0, file.getName().length() - 4 );
90 }
91
92
93 /**
94 * Save all the modifications done to this game ROMs
95 *
96 * @exception IOException occure when something goes wrong while loading or saving
97 * @since
98 */
99
100 public void saveModifications() throws IOException
101 {
102
103 File tempFile = null;
104 ZipInputStream in = null;
105 ZipOutputStream out = null;
106 try
107 {
108
109 tempFile = File.createTempFile( "__romzinger", ".tmp", file.getParentFile() );
110 in = new ZipInputStream( new FileInputStream( file ) );
111 out = new ZipOutputStream( new FileOutputStream( tempFile ) );
112
113 byte[] buffer = new byte[1024];
114
115 boolean firstEntry = true;
116 while ( true )
117 {
118 ZipEntry entry = in.getNextEntry();
119 if ( null == entry ) // we have a zip issue
120 {
121 if ( firstEntry )
122 {
123 throw new MalformedZipFileException();
124 }
125 else // end of the file
126 {
127 break;
128 }
129 }
130 firstEntry = false;
131
132 long crc = entry.getCrc();
133 if ( -1 == crc ) // some 'malformed' zip file are bad handled by java
134 {
135 crc = getCrc( entry.getName() );
136 }
137 ROM rom = ( ROM ) getROM( crc );
138
139 if ( null == rom ) // we have a zip issue
140 {
141 throw new MalformedZipFileException();
142 }
143
144 if ( rom.getUseless() ) // dont copy useless file
145 {
146 continue;
147 }
148
149 ZipEntry newEntry = new ZipEntry( rom.getName() );
150 out.putNextEntry( newEntry );
151
152 int c = 0;
153 while ( ( c = in.read( buffer, 0, 1024 ) ) != -1 )
154 {
155 out.write( buffer, 0, c );
156 }
157 }
158
159 if ( null != in )
160 {
161 in.close();
162 }
163
164 if ( null != out )
165 {
166 out.close();
167 }
168
169 file.delete();
170 tempFile.renameTo( file );
171
172 }
173 catch ( IOException ioe )
174 {
175
176 if ( null != in )
177 {
178 in.close();
179 }
180
181 if ( null != out )
182 {
183 if ( ioe instanceof MalformedZipFileException )
184 {
185 ZipEntry newEntry = new ZipEntry( "dummy" );
186 out.putNextEntry( newEntry );
187 }
188 out.close();
189 }
190
191
192 System.out.println( tempFile.delete() );
193 throw ioe;
194 }
195 }
196
197
198
199 /**
200 * Rename this game
201 *
202 * @param name the new name
203 * @since
204 */
205 public void renameTo( String name )
206 {
207 getFile().renameTo( new File( getFile().getParent(), name + ".zip" ) );
208 }
209
210
211 /**
212 * Get the game information from his file
213 *
214 * @param file the game file
215 * @exception IOException occure when something goes wrong while loading or saving
216 * @since
217 */
218 protected void load( File file )
219 throws IOException
220 {
221 ZipFile zipFile = null;
222 try
223 {
224 this.file = file;
225 zipFile = new ZipFile( file );
226 for ( Enumeration e = zipFile.entries(); e.hasMoreElements(); )
227 {
228 ZipEntry entry = ( ZipEntry ) e.nextElement();
229 ROM rom = new ROM();
230 rom.setName( entry.getName() );
231 long crc = entry.getCrc();
232 crcTable.put( entry.getName(), new Long( crc ) );
233 rom.setCrc( crc );
234 addROM( rom );
235 }
236 }
237 finally
238 {
239 if ( null != zipFile )
240 {
241 zipFile.close();
242 }
243 }
244 }
245
246
247 /**
248 * During the saveModifications remember the crc value for that file
249 * in case ZipInputStream is unable to figure it because the zip file is a
250 * malformed.
251 *
252 * @param name Description of Parameter
253 * @return The Crc value
254 * @since
255 */
256 private long getCrc( String name )
257 {
258 Long crc = ( Long ) crcTable.get( name );
259 return null == crc ? -1 : crc.longValue();
260 }
261
262 }
263
264