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 enum Operation
{ Append
, Create
};
31 explicit nsGZFileWriter(Operation aMode
= Create
);
33 NS_INLINE_DECL_REFCOUNTING(nsGZFileWriter
)
36 * Initialize this object. We'll write our gzip'ed data to the given file,
37 * overwriting its contents if the file exists.
39 * Init() will return an error if called twice. It's an error to call any
40 * other method on this interface without first calling Init().
42 [[nodiscard
]] nsresult
Init(nsIFile
* aFile
);
45 * Alternate version of Init() for use when the file is already opened;
46 * e.g., with a FileDescriptor passed over IPC.
48 [[nodiscard
]] nsresult
InitANSIFileDesc(FILE* aFile
);
51 * Write the given string to the file.
53 [[nodiscard
]] nsresult
Write(const nsACString
& aStr
);
56 * Write |length| bytes of |str| to the file.
58 [[nodiscard
]] nsresult
Write(const char* aStr
, uint32_t aLen
) {
59 return Write(nsDependentCSubstring(aStr
, aLen
));
63 * Close this nsGZFileWriter. This method will be run when the underlying
64 * object is destroyed, so it's not strictly necessary to explicitly call it
67 * It's an error to call this method twice, and it's an error to call Write()
68 * after Finish() has been called.