3 Replies - 980 Views - Last Post: 12 July 2011 - 01:44 PM Rate Topic: -----

#1 jaas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-July 11

Trying to append a jar and a zip file together, get error

Posted 12 July 2011 - 12:10 PM

I am trying to combine a jar file and a zip file together like this:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class append {

    
    private static final byte[] BUFFER = new byte[4096*1024];
    private ArrayList<String> files = new ArrayList<String>();

    public void copy(InputStream input, OutputStream output) throws IOException {
        int bytesRead;
        while ((bytesRead = input.read(BUFFER))!= -1) {
            output.write(BUFFER, 0, bytesRead);
        }
    }
    
    public boolean check(JarEntry jar, JarFile appendfile) {
    	Enumeration<? extends JarEntry> zipentries = appendfile.entries();
    	while(zipentries.hasMoreElements()) {
    		JarEntry e = zipentries.nextElement();
    		if(jar.getName()==e.getName()) {
    			return true;
    		}
    		
    	}
    	return false;
    }

    public void Zip() throws Exception {
    	File directory = new File("C:/jtest/");
    	File jarfile = new File(directory,"test.jar");
    	File filetoappend = new File(directory,"test.zip");
    	File appendedfile = new File(directory,"output.jar");
        JarFile zip = new JarFile(jarfile);
        JarFile appendfile = new JarFile(filetoappend);
        JarOutputStream append = new JarOutputStream(new FileOutputStream(appendedfile));
        
        Enumeration<? extends ZipEntry> fileentries = appendfile.entries();
        
        while(fileentries.hasMoreElements()) {
        	ZipEntry e = fileentries.nextElement();
        	files.add(e.getName());
        }
        fileentries=null;
        
        Enumeration<? extends JarEntry> jarentries = zip.entries();
        while (jarentries.hasMoreElements()) {
            JarEntry e = jarentries.nextElement();
            boolean isnotinFile = true;
            for(String x:files) {
            	if(e.getName().equals(x)) {
            		isnotinFile=false;
            		break;
            	}
            }
            if(isnotinFile) {
		        System.out.println("Copying: " + e.getName());
		        append.putNextEntry(e);
		        if (!e.isDirectory()) {
		            copy(zip.getInputStream(e), append);
		        }
		        append.closeEntry();
            }
        }
        
        Enumeration<? extends JarEntry> zipentries = appendfile.entries();
        
        while (zipentries.hasMoreElements()) {
            JarEntry d = zipentries.nextElement();
            System.out.println("Appending: " + d.getName());
            append.putNextEntry(d);
            if (!d.isDirectory()) {
                copy(appendfile.getInputStream(d), append);
            }
            append.closeEntry();
        }
        

        
        zip.close();
        appendfile.close();
        append.close();
    }
    
    public static void main(String[] args) {
    	try {
			new append().Zip();
		} catch (Exception ex) {
			System.err.println(ex.getMessage());
			ex.printStackTrace();
		}
    }
}


However, when i run, i get this error:
invalid entry compressed size (expected 1232 but got 1227 bytes)
java.util.zip.ZipException: invalid entry compressed size (expected 1232 but got 1227 bytes)
	at java.util.zip.ZipOutputStream.closeEntry(Unknown Source)
	at zip.append.Zip(append.java:91)
	at zip.append.main(append.java:106)


It is notable, that it manages to copy the original file fine, but once it starts on the zip file...

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to append a jar and a zip file together, get error

#2 jaas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-July 11

Re: Trying to append a jar and a zip file together, get error

Posted 12 July 2011 - 12:47 PM

It also manages to replace a single class file fine, but it doesnt seem to be able to copy a class file over
Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Trying to append a jar and a zip file together, get error

Posted 12 July 2011 - 01:22 PM

Have u looked into THIS
Was This Post Helpful? 0
  • +
  • -

#4 jaas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-July 11

Re: Trying to append a jar and a zip file together, get error

Posted 12 July 2011 - 01:44 PM

View Postnick2price, on 12 July 2011 - 01:22 PM, said:

Have u looked into THIS

Actually, yes i have, it isnt very helpful i find. I have also tried the zip file as a zip entry, it still returns the same error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1