Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.drools.guvnor.server.files;
 
 
This Class Allows us to Zip a set of Assets, in fact we generate an extra asset whith the "zip" format, so we can add it to the package.
public class AssetZipper {
    private final List<AssetItemassets;
    public AssetZipper(List<AssetItemassets) {
        this. = assets;
    }
    
    public AssetZipper(List<AssetItemassets,
                       ModuleItem pkg) {
        this. = assets;
        //add pkg when it's really needed - zenix
    }
    public InputStream zipAssets() {
        AssetItem zipFileElement = null;
        BufferedInputStream inputZip = null;
        Iterator<AssetItemit = .iterator();
        byte[] data = new byte[1000];
        int count = 0;
        try {
            if (.size() > 1) {
                ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
                ZipOutputStream outputZip = new ZipOutputStream(outputBytes);
                while (it.hasNext()) {
                    zipFileElement = it.next();
                    inputZip = new BufferedInputStream(zipFileElement.getBinaryContentAttachment());
                    
                    String fileName = null;
                    String binaryContentAttachmentFileName = zipFileElement.getBinaryContentAttachmentFileName();                    
                    //Note the file extension name may not be same as asset format name in some cases.
                    if(binaryContentAttachmentFileName !=null && !"".equals(binaryContentAttachmentFileName)) {
                        fileName = binaryContentAttachmentFileName;
                    } else {
                        fileName = zipFileElement.getName() + "." + zipFileElement.getFormat();
                    }
                    outputZip.putNextEntry(new ZipEntry(fileName));
                    while ((count = inputZip.read(data,
                            0,
                            1000)) != -1) {
                        outputZip.write(data,
                                0,
                                count);
                    }
                    outputZip.flush();
                    inputZip.close();
                }
                outputZip.close();
                return new ByteArrayInputStream(outputBytes.toByteArray());
            } else {
                return it.next().getBinaryContentAttachment();
            }
        } catch (Exception ex) {
        }
        return null;
    }
New to GrepCode? Check out our FAQ X