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