android: Update app-specific/MIME type icons
[LibreOffice.git] / unotools / source / misc / ZipPackageHelper.cxx
blobd94b431bb6c6b453cb879503d42aebdd51cfc9c2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
30 #include <unotools/ZipPackageHelper.hxx>
31 #include <comphelper/storagehelper.hxx>
32 #include <osl/file.hxx>
33 #include <unotools/streamwrap.hxx>
34 #include <tools/stream.hxx>
35 #include <tools/urlobj.hxx>
37 #include <rtl/uri.hxx>
39 namespace com::sun::star::io { class XInputStream; }
41 using namespace utl;
42 using namespace osl;
43 using namespace comphelper;
44 using namespace com::sun::star;
45 using namespace com::sun::star::lang;
46 using namespace com::sun::star::uno;
47 using namespace com::sun::star::util;
48 using namespace com::sun::star::container;
49 using namespace com::sun::star::beans;
50 using namespace com::sun::star::io;
52 using ::rtl::Uri;
54 ZipPackageHelper::ZipPackageHelper(
55 const Reference< XComponentContext >& rxContext,
56 const OUString& sPackageURL)
57 : mxContext( rxContext )
59 // create the package zip file
60 Sequence< Any > aArguments{
61 Any(sPackageURL),
62 // let ZipPackage be used
63 Any(beans::NamedValue("StorageFormat", Any(OUString(ZIP_STORAGE_FORMAT_STRING))))
66 Reference< XHierarchicalNameAccess > xHNameAccess(
67 mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
68 "com.sun.star.packages.comp.ZipPackage",
69 aArguments, mxContext ), UNO_QUERY);
70 mxHNameAccess = xHNameAccess;
72 if( !mxHNameAccess.is() )
73 return;
75 Reference<XSingleServiceFactory> xFactory(mxHNameAccess, UNO_QUERY);
76 mxFactory = xFactory;
78 // get root zip folder
79 mxHNameAccess->getByHierarchicalName( "/" ) >>= mxRootFolder;
82 static OUString encodeZipUri( const OUString& rURI )
84 return Uri::encode( rURI, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8 );
87 Reference< XInterface >& ZipPackageHelper::getRootFolder()
89 return mxRootFolder;
92 Reference< XInterface > ZipPackageHelper::addFolder( Reference< XInterface > const & xRootFolder,
93 const OUString& rName )
95 if ( rName == ".." || rName == "." )
96 throw lang::IllegalArgumentException();
98 Reference< XInterface > xFolder( mxFactory->createInstanceWithArguments({ Any(true) } ));
99 Reference< XNamed > xNamed( xFolder, UNO_QUERY );
100 Reference< XChild > xChild( xFolder, UNO_QUERY );
102 if( xNamed.is() && xChild.is() )
104 OUString aName( encodeZipUri( rName ) );
105 xNamed->setName( aName );
106 xChild->setParent( xRootFolder );
109 return xFolder;
112 void ZipPackageHelper::addFolderWithContent( Reference< XInterface > const & xRootFolder, const OUString& rDirURL )
114 if (rDirURL.isEmpty())
115 return;
117 osl::Directory aDirectory(rDirURL);
119 if (aDirectory.open() != osl::FileBase::E_None)
120 return;
122 osl::DirectoryItem aDirectoryItem;
124 while (osl::FileBase::E_None == aDirectory.getNextItem(aDirectoryItem))
126 osl::FileStatus aFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL | osl_FileStatus_Mask_FileName);
128 if (osl::FileBase::E_None == aDirectoryItem.getFileStatus(aFileStatus))
130 if (aFileStatus.isDirectory())
132 const OUString aFileName(aFileStatus.getFileName());
134 if (!aFileName.isEmpty())
136 Reference<XInterface> folder(addFolder(xRootFolder, aFileName));
137 addFolderWithContent(folder, aFileStatus.getFileURL());
140 else if (aFileStatus.isRegular())
142 addFile(xRootFolder, aFileStatus.getFileURL());
148 void ZipPackageHelper::addFile( css::uno::Reference< css::uno::XInterface > const & xRootFolder,
149 const OUString& rSourceFileURL )
151 INetURLObject aURL( rSourceFileURL );
152 OUString aName( aURL.getName() );
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;
161 Reference< XNameContainer > xNameContainer(xRootFolder, UNO_QUERY );
162 xNameContainer->insertByName(encodeZipUri( aName ), Any(xSink));
163 xSink->setInputStream( xInput );
166 void ZipPackageHelper::savePackage()
168 Reference< XChangesBatch > xBatch( mxHNameAccess, UNO_QUERY );
169 if( xBatch.is() )
170 xBatch->commitChanges();
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */