summaryrefslogtreecommitdiffstats
path: root/unotools/source/misc/ZipPackageHelper.cxx
blob: c66db01272fe0a200cb8830700e71b2a4f767839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <com/sun/star/io/XActiveDataSink.hpp>
#include <com/sun/star/beans/NamedValue.hpp>
#include <com/sun/star/container/XNamed.hpp>
#include <com/sun/star/container/XChild.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/util/XChangesBatch.hpp>
#include <com/sun/star/lang/XUnoTunnel.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>

#include <unotools/ZipPackageHelper.hxx>
#include <comphelper/storagehelper.hxx>
#include <osl/file.hxx>
#include <unotools/streamwrap.hxx>
#include <tools/stream.hxx>
#include <tools/urlobj.hxx>

#include <rtl/uri.hxx>

namespace com::sun::star::io { class XInputStream; }

using namespace utl;
using namespace osl;
using namespace comphelper;
using namespace com::sun::star;
using namespace com::sun::star::lang;
using namespace com::sun::star::uno;
using namespace com::sun::star::util;
using namespace com::sun::star::container;
using namespace com::sun::star::beans;
using namespace com::sun::star::io;

using ::rtl::Uri;

ZipPackageHelper::ZipPackageHelper(
    const Reference< XComponentContext >& rxContext,
    const OUString& sPackageURL)
: mxContext( rxContext )
{
    // create the package zip file
    Sequence< Any > aArguments{
        Any(sPackageURL),
        // let ZipPackage be used
        Any(beans::NamedValue("StorageFormat", Any(OUString(ZIP_STORAGE_FORMAT_STRING))))
    };

    Reference< XHierarchicalNameAccess > xHNameAccess(
        mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
            "com.sun.star.packages.comp.ZipPackage",
            aArguments, mxContext ), UNO_QUERY);
    mxHNameAccess = xHNameAccess;

    if( !mxHNameAccess.is() )
        return;

    Reference<XSingleServiceFactory> xFactory(mxHNameAccess, UNO_QUERY);
    mxFactory = xFactory;

    // get root zip folder
    mxHNameAccess->getByHierarchicalName( "/" ) >>= mxRootFolder;
}

static OUString encodeZipUri( const OUString& rURI )
{
    return Uri::encode( rURI, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8 );
}

Reference< XInterface >& ZipPackageHelper::getRootFolder()
{
    return mxRootFolder;
}

Reference< XInterface > ZipPackageHelper::addFolder( Reference< XInterface > const & xRootFolder,
                                                     const OUString& rName )
{
    if ( rName == ".." || rName == "." )
        throw lang::IllegalArgumentException();

    Reference< XInterface > xFolder( mxFactory->createInstanceWithArguments({ Any(true) } ));
    Reference< XNamed > xNamed( xFolder, UNO_QUERY );
    Reference< XChild > xChild( xFolder, UNO_QUERY );

    if( xNamed.is() && xChild.is() )
    {
        OUString aName( encodeZipUri( rName ) );
        xNamed->setName( aName );
        xChild->setParent( xRootFolder );
    }

    return xFolder;
}

void ZipPackageHelper::addFolderWithContent( Reference< XInterface > const & xRootFolder, const OUString& rDirURL )
{
    if (rDirURL.isEmpty())
        return;

    osl::Directory aDirectory(rDirURL);

    if (aDirectory.open() != osl::FileBase::E_None)
        return;

    osl::DirectoryItem aDirectoryItem;

    while (osl::FileBase::E_None == aDirectory.getNextItem(aDirectoryItem))
    {
        osl::FileStatus aFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL | osl_FileStatus_Mask_FileName);

        if (osl::FileBase::E_None == aDirectoryItem.getFileStatus(aFileStatus))
        {
            if (aFileStatus.isDirectory())
            {
                const OUString aFileName(aFileStatus.getFileName());

                if (!aFileName.isEmpty())
                {
                    Reference<XInterface> folder(addFolder(xRootFolder, aFileName));
                    addFolderWithContent(folder, aFileStatus.getFileURL());
                }
            }
            else if (aFileStatus.isRegular())
            {
                addFile(xRootFolder, aFileStatus.getFileURL());
            }
        }
    }
}

void ZipPackageHelper::addFile( css::uno::Reference< css::uno::XInterface > const & xRootFolder,
                                const OUString& rSourceFileURL )
{
    INetURLObject aURL( rSourceFileURL );
    OUString aName( aURL.getName() );

    SvFileStream* pStream = new SvFileStream(rSourceFileURL, StreamMode::READ );
    Reference< XInputStream > xInput(  new utl::OSeekableInputStreamWrapper( pStream, true ) );
    Reference< XActiveDataSink > xSink( mxFactory->createInstance(), UNO_QUERY );
    Reference< XUnoTunnel > xTunnel( xSink, UNO_QUERY );
    if( !xSink.is() || !xTunnel.is())
        return;

    Reference< XNameContainer > xNameContainer(xRootFolder, UNO_QUERY );
    xNameContainer->insertByName(encodeZipUri( aName ), Any(xTunnel));
    xSink->setInputStream( xInput );
}

void ZipPackageHelper::savePackage()
{
    Reference< XChangesBatch > xBatch( mxHNameAccess, UNO_QUERY );
    if( xBatch.is() )
        xBatch->commitChanges();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */