LibreOffice Module unotools (master) 1
ZipPackageHelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <com/sun/star/io/XActiveDataSink.hpp>
21#include <com/sun/star/beans/NamedValue.hpp>
22#include <com/sun/star/container/XNamed.hpp>
23#include <com/sun/star/container/XChild.hpp>
24#include <com/sun/star/container/XNameContainer.hpp>
25#include <com/sun/star/util/XChangesBatch.hpp>
26#include <com/sun/star/uno/XComponentContext.hpp>
27#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
28#include <com/sun/star/lang/XSingleServiceFactory.hpp>
29
32#include <osl/file.hxx>
34#include <tools/stream.hxx>
35#include <tools/urlobj.hxx>
36
37#include <rtl/uri.hxx>
38
39namespace com::sun::star::io { class XInputStream; }
40
41using namespace utl;
42using namespace osl;
43using namespace comphelper;
44using namespace com::sun::star;
45using namespace com::sun::star::lang;
46using namespace com::sun::star::uno;
47using namespace com::sun::star::util;
48using namespace com::sun::star::container;
49using namespace com::sun::star::beans;
50using namespace com::sun::star::io;
51
52using ::rtl::Uri;
53
54ZipPackageHelper::ZipPackageHelper(
55 const Reference< XComponentContext >& rxContext,
56 const OUString& sPackageURL)
57: mxContext( rxContext )
58{
59 // create the package zip file
61 Any(sPackageURL),
62 // let ZipPackage be used
63 Any(beans::NamedValue("StorageFormat", Any(OUString(ZIP_STORAGE_FORMAT_STRING))))
64 };
65
67 mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
68 "com.sun.star.packages.comp.ZipPackage",
69 aArguments, mxContext ), UNO_QUERY);
70 mxHNameAccess = xHNameAccess;
71
72 if( !mxHNameAccess.is() )
73 return;
74
77
78 // get root zip folder
79 mxHNameAccess->getByHierarchicalName( "/" ) >>= mxRootFolder;
80}
81
82static OUString encodeZipUri( const OUString& rURI )
83{
84 return Uri::encode( rURI, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8 );
85}
86
88{
89 return mxRootFolder;
90}
91
93 const OUString& rName )
94{
95 if ( rName == ".." || rName == "." )
96 throw lang::IllegalArgumentException();
97
98 Reference< XInterface > xFolder( mxFactory->createInstanceWithArguments({ Any(true) } ));
99 Reference< XNamed > xNamed( xFolder, UNO_QUERY );
100 Reference< XChild > xChild( xFolder, UNO_QUERY );
101
102 if( xNamed.is() && xChild.is() )
103 {
104 OUString aName( encodeZipUri( rName ) );
105 xNamed->setName( aName );
106 xChild->setParent( xRootFolder );
107 }
108
109 return xFolder;
110}
111
112void ZipPackageHelper::addFolderWithContent( Reference< XInterface > const & xRootFolder, const OUString& rDirURL )
113{
114 if (rDirURL.isEmpty())
115 return;
116
117 osl::Directory aDirectory(rDirURL);
118
119 if (aDirectory.open() != osl::FileBase::E_None)
120 return;
121
122 osl::DirectoryItem aDirectoryItem;
123
124 while (osl::FileBase::E_None == aDirectory.getNextItem(aDirectoryItem))
125 {
126 osl::FileStatus aFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL | osl_FileStatus_Mask_FileName);
127
128 if (osl::FileBase::E_None == aDirectoryItem.getFileStatus(aFileStatus))
129 {
130 if (aFileStatus.isDirectory())
131 {
132 const OUString aFileName(aFileStatus.getFileName());
133
134 if (!aFileName.isEmpty())
135 {
136 Reference<XInterface> folder(addFolder(xRootFolder, aFileName));
137 addFolderWithContent(folder, aFileStatus.getFileURL());
138 }
139 }
140 else if (aFileStatus.isRegular())
141 {
142 addFile(xRootFolder, aFileStatus.getFileURL());
143 }
144 }
145 }
146}
147
148void ZipPackageHelper::addFile( css::uno::Reference< css::uno::XInterface > const & xRootFolder,
149 const OUString& rSourceFileURL )
150{
151 INetURLObject aURL( rSourceFileURL );
152 OUString aName( aURL.getName() );
153
154 SvFileStream* pStream = new SvFileStream(rSourceFileURL, StreamMode::READ );
155 Reference< XInputStream > xInput( new utl::OSeekableInputStreamWrapper( pStream, true ) );
156 Reference< XActiveDataSink > xSink( mxFactory->createInstance(), UNO_QUERY );
157 assert(xSink); // this should never fail
158 if( !xSink.is() )
159 return;
160
161 Reference< XNameContainer > xNameContainer(xRootFolder, UNO_QUERY );
162 xNameContainer->insertByName(encodeZipUri( aName ), Any(xSink));
163 xSink->setInputStream( xInput );
164}
165
167{
168 Reference< XChangesBatch > xBatch( mxHNameAccess, UNO_QUERY );
169 if( xBatch.is() )
170 xBatch->commitChanges();
171}
172
173/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static OUString encodeZipUri(const OUString &rURI)
helper class for wrapping an SvStream into a com.sun.star.io::XInputStream which is seekable (i....
Definition: streamwrap.hxx:86
css::uno::Reference< css::lang::XSingleServiceFactory > mxFactory
void addFolderWithContent(css::uno::Reference< css::uno::XInterface > const &xRootFolder, const OUString &rDirURL)
css::uno::Reference< css::container::XHierarchicalNameAccess > mxHNameAccess
css::uno::Reference< css::uno::XInterface > mxRootFolder
css::uno::Reference< css::uno::XInterface > addFolder(css::uno::Reference< css::uno::XInterface > const &xRootFolder, const OUString &rName)
void addFile(css::uno::Reference< css::uno::XInterface > const &xRootFolder, const OUString &rSourceFile)
css::uno::Reference< css::uno::XInterface > & getRootFolder()
css::uno::Reference< css::uno::XComponentContext > mxContext
URL aURL
uno::Reference< uno::XComponentContext > mxContext
Reference< XSingleServiceFactory > xFactory
Sequence< PropertyValue > aArguments
OUString aName
tools::SvRef< SvBaseLink > xSink
constexpr OUStringLiteral ZIP_STORAGE_FORMAT_STRING