1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
19 #ifndef INCLUDED_PACKAGE_INC_ZIPOUTPUTENTRY_HXX
20 #define INCLUDED_PACKAGE_INC_ZIPOUTPUTENTRY_HXX
22 #include <com/sun/star/io/XOutputStream.hpp>
23 #include <com/sun/star/io/XTempFile.hpp>
24 #include <com/sun/star/uno/Reference.hxx>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <com/sun/star/xml/crypto/XCipherContext.hpp>
27 #include <com/sun/star/xml/crypto/XDigestContext.hpp>
29 #include <package/Deflater.hxx>
30 #include <comphelper/threadpool.hxx>
31 #include <unotools/tempfile.hxx>
37 class ZipPackageBuffer
;
38 class ZipPackageStream
;
40 class ZipOutputEntryBase
43 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
44 css::uno::Reference
< css::io::XOutputStream
> m_xOutStream
;
46 css::uno::Reference
< css::xml::crypto::XCipherContext
> m_xCipherContext
;
47 css::uno::Reference
< css::xml::crypto::XDigestContext
> m_xDigestContext
;
50 ZipEntry
*m_pCurrentEntry
;
51 sal_Int16 m_nDigested
;
52 ZipPackageStream
* m_pCurrentStream
;
53 bool m_bEncryptCurrentEntry
;
56 virtual ~ZipOutputEntryBase() = default;
58 virtual void writeStream(const css::uno::Reference
< css::io::XInputStream
>& xInStream
) = 0;
60 ZipEntry
* getZipEntry() { return m_pCurrentEntry
; }
61 ZipPackageStream
* getZipPackageStream() { return m_pCurrentStream
; }
62 bool isEncrypt() const { return m_bEncryptCurrentEntry
; }
68 css::uno::Reference
< css::io::XOutputStream
> xOutStream
,
69 css::uno::Reference
< css::uno::XComponentContext
> xContext
,
70 ZipEntry
& rEntry
, ZipPackageStream
* pStream
, bool bEncrypt
, bool checkStream
);
72 // Inherited classes call this with deflated data buffer.
73 void processDeflated( const css::uno::Sequence
< sal_Int8
>& deflateBuffer
, sal_Int32 nLength
);
74 // Inherited classes call this with the input buffer.
75 void processInput( const css::uno::Sequence
< sal_Int8
>& rBuffer
);
77 virtual void finishDeflater() = 0;
78 virtual sal_Int64
getDeflaterTotalIn() const = 0;
79 virtual sal_Int64
getDeflaterTotalOut() const = 0;
80 virtual void deflaterReset() = 0;
81 virtual bool isDeflaterFinished() const = 0;
84 // Normal non-threaded case.
85 class ZipOutputEntry
: public ZipOutputEntryBase
87 css::uno::Sequence
< sal_Int8
> m_aDeflateBuffer
;
88 ZipUtils::Deflater m_aDeflater
;
92 const css::uno::Reference
< css::io::XOutputStream
>& rxOutStream
,
93 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
94 ZipEntry
& rEntry
, ZipPackageStream
* pStream
, bool bEncrypt
);
95 void writeStream(const css::uno::Reference
< css::io::XInputStream
>& xInStream
) override
;
96 void write(const css::uno::Sequence
< sal_Int8
>& rBuffer
);
100 const css::uno::Reference
< css::io::XOutputStream
>& rxOutStream
,
101 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
102 ZipEntry
& rEntry
, ZipPackageStream
* pStream
, bool bEncrypt
, bool checkStream
);
103 virtual void finishDeflater() override
;
104 virtual sal_Int64
getDeflaterTotalIn() const override
;
105 virtual sal_Int64
getDeflaterTotalOut() const override
;
106 virtual void deflaterReset() override
;
107 virtual bool isDeflaterFinished() const override
;
111 // Class that runs the compression in a background thread.
112 class ZipOutputEntryInThread final
: public ZipOutputEntry
115 rtl::Reference
<utl::TempFileFastService
> m_xTempFile
;
116 std::exception_ptr m_aParallelDeflateException
;
117 std::atomic
<bool> m_bFinished
;
120 ZipOutputEntryInThread(
121 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
122 ZipEntry
& rEntry
, ZipPackageStream
* pStream
, bool bEncrypt
);
123 std::unique_ptr
<comphelper::ThreadTask
> createTask(
124 const std::shared_ptr
<comphelper::ThreadTaskTag
>& pTag
,
125 const css::uno::Reference
< css::io::XInputStream
>& xInStream
);
126 /* This block of methods is for threaded zipping, where we compress to a temp stream, whose
127 data is retrieved via getData */
128 void createBufferFile();
129 void setParallelDeflateException(const std::exception_ptr
& exception
) { m_aParallelDeflateException
= exception
; }
130 css::uno::Reference
< css::io::XInputStream
> getData() const;
131 const std::exception_ptr
& getParallelDeflateException() const { return m_aParallelDeflateException
; }
132 void closeBufferFile();
133 void deleteBufferFile();
134 bool isFinished() const { return m_bFinished
; }
136 void setFinished() { m_bFinished
= true; }
139 // Class that synchronously runs the compression in multiple threads (using ThreadDeflater).
140 class ZipOutputEntryParallel final
: public ZipOutputEntryBase
146 ZipOutputEntryParallel(
147 const css::uno::Reference
< css::io::XOutputStream
>& rxOutStream
,
148 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
149 ZipEntry
& rEntry
, ZipPackageStream
* pStream
, bool bEncrypt
);
150 void writeStream(const css::uno::Reference
< css::io::XInputStream
>& xInStream
) override
;
152 virtual void finishDeflater() override
;
153 virtual sal_Int64
getDeflaterTotalIn() const override
;
154 virtual sal_Int64
getDeflaterTotalOut() const override
;
155 virtual void deflaterReset() override
;
156 virtual bool isDeflaterFinished() const override
;
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */