Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I just read about zip bombs, i.e. zip files that contain very large amount of highly compressible data (00000000000000000...). When opened they fill the server's disk. How can I detect a zip file is a zip bomb before unzipping it? UPDATE Can you tell me how is this done in Python or Java?
It's been a while since I've done Java I/O, and I'm not aware of the latest "right" ways to work with Zip and GZip files. I don't necessarily need a full working demo - I'm primarily looking for the right interfaces and methods to be using. Yes, I could look up any random tutorial on this, but performance is an issue (these files can get pretty big) and I do care about using the best tool for t...
I'm stuck with this junit test: public void test() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zipOut = new ZipOutputStream( out ); zipOut.putNextEntry( new ZipEntry( "file" ) ); zipOut.write( (new byte[] { 0x01, 0x02, 0x03 }) ); zipOut.closeEntry(); zipOut.close(); ZipInputStream zipIn = new ZipInputStream( new ByteA...
Any suggestions for a tool/object/utility to read a .zipx file in java? Have already looked at http://stackoverflow.com/questions/1200289/i-need-a-c-library-for-zipx and http://www.winzip.com/comp_info.htm
I cannot use third party software/libraries and have a zip file that I have to read in WPF application and get the binary data (that represents the image). That zip file contains about 7000 images and I have to find the right one by its name. How can I get the ZipEntry (analog in Java)? http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/ZipEntry.html Sincerely, Roman
I'm working on digital documents and digital signatures and I've stumbled upon a problem. Input: documentX.adoc - zip file with files and folders inside. I need to get all the content in the input file - a list of dirs and files. What do I do: ZipFile adocFile = new ZipFile(documentXFileName); ArrayList <String> adocFiles = new ArrayList<String>(); Enumeration <? extends ZipE...
I currently am tasked with updating an XML file (persistance.xml) within a jar at a customers site. I can of course unjar the file, update the xml, then rejar the file for redeployment. I would like to kind these command line operations in a Swing App so that the person doing it does not have to drop to the command line. Any thoughts on a way to do this programatically?
I asked a question earlier about unzip/zip using windows cmd command in Java. That turn out to be not good for a program and one would suggest using the java.util.zip to unzip/zip. After looking through some tutorial, I found out usually they just loop through the content of the zip file. If I know the name of the file I want, can I extract it explicitly without looping through the whole zip fi...
I create zip file using ZipOutputStream. I put in the zip one file(both file and zip are in the same dir), however the file is stored with fullpath (C:\TEMP\file.xml), how to store it with relative or no path?
public static void writeFile(String theFileName, String theFilePath) { try { File currentFile = new File("plugins/mcMMO/Resources/"+theFilePath+theFileName); //System.out.println(theFileName); @SuppressWarnings("static-access") JarFile jar = new JarFile(plugin.mcmmo); JarEntry entry = jar.getJarEntry("resources/"+theFileName); InputStream is =...
I am having a zip file with lot of xml files. I am having some data which I have to check with the xml files content and which ever xml file content matches with that data I have to extract that file only and store that in a folder. Guide me to achieve this. Thanking you. --raaja.g
I am trying to store dates as latest modification timestamp in a ZIP -file. It seems that ZIP format support only dates after 1980-01-01 as a last modification time (at least via Java API java.util.zip.ZipEntry ) Is this correct? Is the earliest supported modification timestamp really 1980-01-01 00:00:00? I tried to find some references to verify this but I couldn't find any.
  /*
   * Copyright 1995-2005 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.util.zip;
 
 import java.util.Date;

This class is used to represent a ZIP file entry.

Author(s):
David Connelly
 
 public
 class ZipEntry implements ZipConstantsCloneable {
     String name;        // entry name
     long time = -1;     // modification time (in DOS time)
     long crc = -1;      // crc-32 of entry data
     long size = -1;     // uncompressed size of entry data
     long csize = -1;    // compressed size of entry data
     int method = -1;    // compression method
     byte[] extra;       // optional extra field data for entry
     String comment;     // optional comment string for entry
 
    
Compression method for uncompressed entries.
 
     public static final int STORED = 0;

    
Compression method for compressed (deflated) entries.
 
     public static final int DEFLATED = 8;
 
     static {
         /* Zip library is loaded from System.initializeSystemClass */
         initIDs();
     }
 
     private static native void initIDs();

    
Creates a new zip entry with the specified name.

Parameters:
name the entry name
Throws:
java.lang.NullPointerException if the entry name is null
java.lang.IllegalArgumentException if the entry name is longer than 0xFFFF bytes
 
     public ZipEntry(String name) {
         if (name == null) {
             throw new NullPointerException();
         }
         if (name.length() > 0xFFFF) {
             throw new IllegalArgumentException("entry name too long");
         }
         this. = name;
     }

    
Creates a new zip entry with fields taken from the specified zip entry.

Parameters:
e a zip Entry object
 
     public ZipEntry(ZipEntry e) {
          = e.name;
          = e.time;
          = e.crc;
          = e.size;
          = e.csize;
          = e.method;
          = e.extra;
          = e.comment;
     }
 
     /*
      * Creates a new zip entry for the given name with fields initialized
      * from the specified jzentry data.
     */
    ZipEntry(String namelong jzentry) {
        this. = name;
        initFields(jzentry);
    }
    private native void initFields(long jzentry);
    /*
     * Creates a new zip entry with fields initialized from the specified
     * jzentry data.
     */
    ZipEntry(long jzentry) {
        initFields(jzentry);
    }

    
Returns the name of the entry.

Returns:
the name of the entry
    public String getName() {
        return ;
    }

    
Sets the modification time of the entry.

Parameters:
time the entry modification time in number of milliseconds since the epoch
See also:
getTime()
    public void setTime(long time) {
        this. = javaToDosTime(time);
    }

    
Returns the modification time of the entry, or -1 if not specified.

Returns:
the modification time of the entry, or -1 if not specified
See also:
setTime(long)
    public long getTime() {
        return  != -1 ? dosToJavaTime() : -1;
    }

    
Sets the uncompressed size of the entry data.

Parameters:
size the uncompressed size in bytes
Throws:
java.lang.IllegalArgumentException if the specified size is less than 0 or greater than 0xFFFFFFFF bytes
See also:
getSize()
    public void setSize(long size) {
        if (size < 0 || size > 0xFFFFFFFFL) {
            throw new IllegalArgumentException("invalid entry size");
        }
        this. = size;
    }

    
Returns the uncompressed size of the entry data, or -1 if not known.

Returns:
the uncompressed size of the entry data, or -1 if not known
See also:
setSize(long)
    public long getSize() {
        return ;
    }

    
Returns the size of the compressed entry data, or -1 if not known. In the case of a stored entry, the compressed size will be the same as the uncompressed size of the entry.

Returns:
the size of the compressed entry data, or -1 if not known
See also:
setCompressedSize(long)
    public long getCompressedSize() {
        return ;
    }

    
Sets the size of the compressed entry data.

Parameters:
csize the compressed size to set to
See also:
getCompressedSize()
    public void setCompressedSize(long csize) {
        this. = csize;
    }

    
Sets the CRC-32 checksum of the uncompressed entry data.

Parameters:
crc the CRC-32 value
Throws:
java.lang.IllegalArgumentException if the specified CRC-32 value is less than 0 or greater than 0xFFFFFFFF
See also:
getCrc()
    public void setCrc(long crc) {
        if (crc < 0 || crc > 0xFFFFFFFFL) {
            throw new IllegalArgumentException("invalid entry crc-32");
        }
        this. = crc;
    }

    
Returns the CRC-32 checksum of the uncompressed entry data, or -1 if not known.

Returns:
the CRC-32 checksum of the uncompressed entry data, or -1 if not known
See also:
setCrc(long)
    public long getCrc() {
        return ;
    }

    
Sets the compression method for the entry.

Parameters:
method the compression method, either STORED or DEFLATED
Throws:
java.lang.IllegalArgumentException if the specified compression method is invalid
See also:
getMethod()
    public void setMethod(int method) {
        if (method !=  && method != ) {
            throw new IllegalArgumentException("invalid compression method");
        }
        this. = method;
    }

    
Returns the compression method of the entry, or -1 if not specified.

Returns:
the compression method of the entry, or -1 if not specified
See also:
setMethod(int)
    public int getMethod() {
        return ;
    }

    
Sets the optional extra field data for the entry.

Parameters:
extra the extra field data bytes
Throws:
java.lang.IllegalArgumentException if the length of the specified extra field data is greater than 0xFFFF bytes
See also:
getExtra()
    public void setExtra(byte[] extra) {
        if (extra != null && extra.length > 0xFFFF) {
            throw new IllegalArgumentException("invalid extra field length");
        }
        this. = extra;
    }

    
Returns the extra field data for the entry, or null if none.

Returns:
the extra field data for the entry, or null if none
See also:
setExtra(byte[])
    public byte[] getExtra() {
        return ;
    }

    
Sets the optional comment string for the entry.

Parameters:
comment the comment string
Throws:
java.lang.IllegalArgumentException if the length of the specified comment string is greater than 0xFFFF bytes
See also:
getComment()
    public void setComment(String comment) {
        if (comment != null && comment.length() > 0xffff/3
                    && ZipOutputStream.getUTF8Length(comment) > 0xffff) {
            throw new IllegalArgumentException("invalid entry comment length");
        }
        this. = comment;
    }

    
Returns the comment string for the entry, or null if none.

Returns:
the comment string for the entry, or null if none
See also:
setComment(java.lang.String)
    public String getComment() {
        return ;
    }

    
Returns true if this is a directory entry. A directory entry is defined to be one whose name ends with a '/'.

Returns:
true if this is a directory entry
    public boolean isDirectory() {
        return .endsWith("/");
    }

    
Returns a string representation of the ZIP entry.
    public String toString() {
        return getName();
    }
    /*
     * Converts DOS time to Java time (number of milliseconds since epoch).
     */
    private static long dosToJavaTime(long dtime) {
        Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
                          (int)(((dtime >> 21) & 0x0f) - 1),
                          (int)((dtime >> 16) & 0x1f),
                          (int)((dtime >> 11) & 0x1f),
                          (int)((dtime >> 5) & 0x3f),
                          (int)((dtime << 1) & 0x3e));
        return d.getTime();
    }
    /*
     * Converts Java time to DOS time.
     */
    private static long javaToDosTime(long time) {
        Date d = new Date(time);
        int year = d.getYear() + 1900;
        if (year < 1980) {
            return (1 << 21) | (1 << 16);
        }
        return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
               d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
               d.getSeconds() >> 1;
    }

    
Returns the hash code value for this entry.
    public int hashCode() {
        return .hashCode();
    }

    
Returns a copy of this entry.
    public Object clone() {
        try {
            ZipEntry e = (ZipEntry)super.clone();
            e.extra = ( == null) ? null : .clone();
            return e;
        } catch (CloneNotSupportedException e) {
            // This should never happen, since we are Cloneable
            throw new InternalError();
        }
    }
New to GrepCode? Check out our FAQ X