2012-08-29 54 views

回答

0

java.util.zipException:太短,拉鍊意味着你的郵編是無效的。請檢查您是否正在創建有效的郵編。

你可以找到Example

我已經驗證和下面的代碼工作。

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipException; 
import java.util.zip.ZipOutputStream; 


public class CreateZipFileDirectory { 

    public static void main(String args[]) 
    {     
      try 
      { 
        String zipFile = "Path where zip needs to be creaetd e.g.zipdemo.zip"; 
        String sourceDirectory = "directory which contains files."; 

        //create byte buffer 
        byte[] buffer = new byte[1024]; 
        /* 
        * To create a zip file, use 
        * 
        * ZipOutputStream(OutputStream out) 
        * constructor of ZipOutputStream class. 
        * 
        */ 

        //create object of FileOutputStream 
        FileOutputStream fout = new FileOutputStream(zipFile); 

        //create object of ZipOutputStream from FileOutputStream 
        ZipOutputStream zout = new ZipOutputStream(fout); 

        //create File object from directory name 
        File dir = new File(sourceDirectory); 

        //check to see if this directory exists 
        if(!dir.isDirectory()) 
        { 
          System.out.println(sourceDirectory + " is not a directory"); 
        } 
        else 
        { 
          File[] files = dir.listFiles(); 

          for(int i=0; i < files.length ; i++) 
          { 
            System.out.println("Adding " + files[i].getName()); 

            //create object of FileInputStream for source file 
            FileInputStream fin = new FileInputStream(files[i]); 

            /* 
            * To begin writing ZipEntry in the zip file, use 
            * 
            * void putNextEntry(ZipEntry entry) 
            * method of ZipOutputStream class. 
            * 
            * This method begins writing a new Zip entry to 
            * the zip file and positions the stream to the start 
            * of the entry data. 
            */ 

            zout.putNextEntry(new ZipEntry(files[i].getName())); 

            /* 
            * After creating entry in the zip file, actually 
            * write the file. 
            */ 
            int length; 

            while((length = fin.read(buffer)) > 0) 
            { 
             zout.write(buffer, 0, length); 
            } 

            /* 
            * After writing the file to ZipOutputStream, use 
            * 
            * void closeEntry() method of ZipOutputStream class to 
            * close the current entry and position the stream to 
            * write the next entry. 
            */ 

            zout.closeEntry(); 
            //close the InputStream 
            fin.close(); 
          } 
        } 

         //close the ZipOutputStream 
         zout.close(); 

         System.out.println("Zip file has been created!"); 

      } 
      catch(IOException ioe) 
      { 
        System.out.println("IOException :" + ioe); 
      } 

    } 

}

/* 
Output of this program would be 
Adding nonav.log 
Adding ospreg.exe 
Adding servers.ini 
Adding setupisam.log 
Adding sourceFile1.doc 
Adding sourceFile2.doc 
Zip file has been created! 
*/ 
0

看看在readCentralDir()梅索德,你將不得不採取更深入地瞭解代碼,但總體看來這個異常被拋出,一旦該文件是太短成爲一個ZIP文件。

如下因素代碼行(mRaf是隨機訪問文件):

long scanOffset = mRaf.length() - ENDHDR; 
if (scanOffset < 0) { 
    throw new ZipException("too short to be Zip"); 
} 
相關問題