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