org.symbian.tools.mtw.core/src/org/symbian/tools/mtw/core/utilities/ZipApplicationVisitor.java
changeset 461 7a8f9fa8d278
parent 460 c0bff5ed874c
child 462 cdc4995b1677
equal deleted inserted replaced
460:c0bff5ed874c 461:7a8f9fa8d278
     1 /**
       
     2  * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  */
       
    19 package org.symbian.tools.mtw.core.utilities;
       
    20 
       
    21 import java.io.IOException;
       
    22 import java.io.InputStream;
       
    23 import java.io.OutputStream;
       
    24 import java.util.zip.ZipEntry;
       
    25 import java.util.zip.ZipOutputStream;
       
    26 
       
    27 import org.eclipse.core.resources.IFile;
       
    28 import org.eclipse.core.resources.IResource;
       
    29 import org.eclipse.core.resources.IResourceVisitor;
       
    30 import org.eclipse.core.runtime.CoreException;
       
    31 import org.eclipse.core.runtime.IPath;
       
    32 import org.eclipse.core.runtime.IStatus;
       
    33 import org.eclipse.core.runtime.Status;
       
    34 import org.symbian.tools.mtw.core.MTWCore;
       
    35 import org.symbian.tools.mtw.core.runtimes.IPackagerDelegate;
       
    36 
       
    37 /**
       
    38  * Use this visitor to zip application if the web runtime uses zip archive as application
       
    39  * distribution format.
       
    40  * 
       
    41  * @author Eugene Ostroukhov (eugeneo@symbian.org)
       
    42  */
       
    43 public class ZipApplicationVisitor implements IResourceVisitor {
       
    44     private static final int DEFAULT_BUFFER_SIZE = 65536;
       
    45     private final ZipOutputStream zipStream;
       
    46     private final IPackagerDelegate packager;
       
    47 
       
    48     public ZipApplicationVisitor(ZipOutputStream zipStream, IPackagerDelegate packager) {
       
    49         this.zipStream = zipStream;
       
    50         this.packager = packager;
       
    51     }
       
    52 
       
    53     public boolean visit(IResource resource) throws CoreException {
       
    54         IPath path = packager.getPathInPackage(resource);
       
    55         if (path != null && resource.getType() == IResource.FILE) {
       
    56             zip(path, resource);
       
    57         }
       
    58         return true;
       
    59     }
       
    60 
       
    61     private void zip(IPath path, IResource file) throws CoreException {
       
    62         ZipEntry zipEntry = new ZipEntry(path.toString());
       
    63         try {
       
    64             zipStream.putNextEntry(zipEntry);
       
    65             if (file.getType() == IResource.FILE) {
       
    66                 InputStream contents = ((IFile) file).getContents();
       
    67                 try {
       
    68                     copy(contents, zipStream);
       
    69                 } finally {
       
    70                     contents.close();
       
    71                 }
       
    72             }
       
    73             zipStream.closeEntry();
       
    74         } catch (IOException e) {
       
    75             throw new CoreException(new Status(IStatus.ERROR, MTWCore.PLUGIN_ID, String.format("Can't package %s", file
       
    76                     .getFullPath().toString()), e));
       
    77         }
       
    78     }
       
    79 
       
    80     public static int copy(InputStream input, OutputStream output) throws IOException {
       
    81         long count = copyLarge(input, output);
       
    82         if (count > Integer.MAX_VALUE) {
       
    83             return -1;
       
    84         }
       
    85         return (int) count;
       
    86     }
       
    87 
       
    88     public static long copyLarge(InputStream input, OutputStream output) throws IOException {
       
    89         byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
       
    90         long count = 0;
       
    91         int n = 0;
       
    92         while (-1 != (n = input.read(buffer))) {
       
    93             output.write(buffer, 0, n);
       
    94             count += n;
       
    95         }
       
    96         return count;
       
    97     }
       
    98 
       
    99 }