src/share/classes/java/util/zip/ZipOutputStream.java

Print this page




 144      * @exception IllegalArgumentException if the specified compression method
 145      *            is invalid
 146      */
 147     public void setMethod(int method) {
 148         if (method != DEFLATED && method != STORED) {
 149             throw new IllegalArgumentException("invalid compression method");
 150         }
 151         this.method = method;
 152     }
 153 
 154     /**
 155      * Sets the compression level for subsequent entries which are DEFLATED.
 156      * The default setting is DEFAULT_COMPRESSION.
 157      * @param level the compression level (0-9)
 158      * @exception IllegalArgumentException if the compression level is invalid
 159      */
 160     public void setLevel(int level) {
 161         def.setLevel(level);
 162     }
 163 
































 164     /**
 165      * Begins writing a new ZIP file entry and positions the stream to the
 166      * start of the entry data. Closes the current entry if still active.
 167      * The default compression method will be used if no compression method
 168      * was specified for the entry, and the current time will be used if
 169      * the entry has no set modification time.
 170      * @param e the ZIP entry to be written
 171      * @exception ZipException if a ZIP format error has occurred
 172      * @exception IOException if an I/O error has occurred
 173      */
 174     public void putNextEntry(ZipEntry e) throws IOException {
 175         ensureOpen();
 176         if (current != null) {
 177             closeEntry();       // close previous entry
 178         }
 179         if (e.time == -1) {
 180             e.setTime(System.currentTimeMillis());
 181         }
 182         if (e.method == -1) {
 183             e.method = method;  // use default method
 184         }
 185         // store size, compressed size, and crc-32 in LOC header
 186         e.flag = 0;
 187         switch (e.method) {
 188         case DEFLATED:
 189             // store size, compressed size, and crc-32 in data descriptor
 190             // immediately following the compressed entry data
 191             if (e.size  == -1 || e.csize == -1 || e.crc   == -1)
 192                 e.flag = 8;
 193 

 194             break;
 195         case STORED:
 196             // compressed size, uncompressed size, and crc-32 must all be
 197             // set for entries using STORED compression method
 198             if (e.size == -1) {
 199                 e.size = e.csize;
 200             } else if (e.csize == -1) {
 201                 e.csize = e.size;
 202             } else if (e.size != e.csize) {
 203                 throw new ZipException(
 204                     "STORED entry where compressed != uncompressed size");
 205             }
 206             if (e.size == -1 || e.crc == -1) {
 207                 throw new ZipException(
 208                     "STORED entry missing size, compressed size, or crc-32");
 209             }
 210             break;
 211         default:
 212             throw new ZipException("unsupported compression method");
 213         }




 144      * @exception IllegalArgumentException if the specified compression method
 145      *            is invalid
 146      */
 147     public void setMethod(int method) {
 148         if (method != DEFLATED && method != STORED) {
 149             throw new IllegalArgumentException("invalid compression method");
 150         }
 151         this.method = method;
 152     }
 153 
 154     /**
 155      * Sets the compression level for subsequent entries which are DEFLATED.
 156      * The default setting is DEFAULT_COMPRESSION.
 157      * @param level the compression level (0-9)
 158      * @exception IllegalArgumentException if the compression level is invalid
 159      */
 160     public void setLevel(int level) {
 161         def.setLevel(level);
 162     }
 163 
 164     public void writeNextEntry(ZipEntry e, byte[] b, int off, int len)
 165         throws IOException
 166     {
 167         ensureOpen();
 168         if (current != null) {
 169             closeEntry();       // close previous entry
 170         }
 171         if (e.time == -1) {
 172             e.setTime(System.currentTimeMillis());
 173         }
 174         if (e.method != DEFLATED) {
 175             throw new ZipException("writeNextEntry: e.method must be DEFLATED");
 176         }
 177         if (e.size  == -1 || e.csize == -1 || e.crc   == -1) {
 178             throw new ZipException("writeNextEntry: e.size/csize/crc is -1");
 179         }
 180         if (! names.add(e.name)) {
 181             throw new ZipException("duplicate entry: " + e.name);
 182         }
 183         if (zc.isUTF8())
 184             e.flag |= EFS;
 185         current = new XEntry(e, written);
 186         xentries.add(current);
 187         writeLOC(current);
 188         out.write(b, off, len);
 189         written += len;
 190         if ((e.flag & 8) != 0)
 191             writeEXT(e);
 192         crc.reset();
 193         current = null;
 194     }
 195 
 196     /**
 197      * Begins writing a new ZIP file entry and positions the stream to the
 198      * start of the entry data. Closes the current entry if still active.
 199      * The default compression method will be used if no compression method
 200      * was specified for the entry, and the current time will be used if
 201      * the entry has no set modification time.
 202      * @param e the ZIP entry to be written
 203      * @exception ZipException if a ZIP format error has occurred
 204      * @exception IOException if an I/O error has occurred
 205      */
 206     public void putNextEntry(ZipEntry e) throws IOException {
 207         ensureOpen();
 208         if (current != null) {
 209             closeEntry();       // close previous entry
 210         }
 211         if (e.time == -1) {
 212             e.setTime(System.currentTimeMillis());
 213         }
 214         if (e.method == -1) {
 215             e.method = method;  // use default method
 216         }
 217         // store size, compressed size, and crc-32 in LOC header
 218         e.flag = 0;
 219         switch (e.method) {
 220         case DEFLATED:
 221             // store size, compressed size, and crc-32 in data descriptor
 222             // immediately following the compressed entry data
 223             if (e.size  == -1 || e.csize == -1 || e.crc   == -1)
 224                 e.flag = 8;
 225 
 226 
 227             break;
 228         case STORED:
 229             // compressed size, uncompressed size, and crc-32 must all be
 230             // set for entries using STORED compression method
 231             if (e.size == -1) {
 232                 e.size = e.csize;
 233             } else if (e.csize == -1) {
 234                 e.csize = e.size;
 235             } else if (e.size != e.csize) {
 236                 throw new ZipException(
 237                     "STORED entry where compressed != uncompressed size");
 238             }
 239             if (e.size == -1 || e.crc == -1) {
 240                 throw new ZipException(
 241                     "STORED entry missing size, compressed size, or crc-32");
 242             }
 243             break;
 244         default:
 245             throw new ZipException("unsupported compression method");
 246         }