1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsGZFileWriter_h
8 #define nsGZFileWriter_h
10 #include "nsISupportsImpl.h"
12 #include "nsDependentString.h"
16 * A simple class for writing to a .gz file.
18 * Note that the file that this interface produces has a different format than
19 * what you'd get if you compressed your data as a gzip stream and dumped the
22 * The standard gunzip tool cannot decompress a raw gzip stream, but can handle
23 * the files produced by this interface.
25 class nsGZFileWriter final
{
26 virtual ~nsGZFileWriter();
29 explicit nsGZFileWriter();
31 NS_INLINE_DECL_REFCOUNTING(nsGZFileWriter
)
34 * Initialize this object. We'll write our gzip'ed data to the given file,
35 * overwriting its contents if the file exists.
37 * Init() will return an error if called twice. It's an error to call any
38 * other method on this interface without first calling Init().
40 [[nodiscard
]] nsresult
Init(nsIFile
* aFile
);
43 * Alternate version of Init() for use when the file is already opened;
44 * e.g., with a FileDescriptor passed over IPC.
46 [[nodiscard
]] nsresult
InitANSIFileDesc(FILE* aFile
);
49 * Write the given string to the file.
51 [[nodiscard
]] nsresult
Write(const nsACString
& aStr
);
54 * Write |length| bytes of |str| to the file.
56 [[nodiscard
]] nsresult
Write(const char* aStr
, uint32_t aLen
) {
57 return Write(nsDependentCSubstring(aStr
, aLen
));
61 * Close this nsGZFileWriter. This method will be run when the underlying
62 * object is destroyed, so it's not strictly necessary to explicitly call it
65 * It's an error to call this method twice, and it's an error to call Write()
66 * after Finish() has been called.
74 z_stream mZStream
= {};