Do not emit compressed files larger than the input #319

Closed
opened 2026-01-29 20:41:57 +00:00 by claunia · 6 comments
Owner

Originally created by @dilyanpalauzov on GitHub (Apr 23, 2020).

As a matter of fact I want to compress all text files on a webserver, so that these are served compressen on Accept-Encoding. As a matter of fact, an uncompressed file of 41 bytes is compressed to 46 bytes. when the output is bigger than the input, the output file shall not be created by default.

Originally created by @dilyanpalauzov on GitHub (Apr 23, 2020). As a matter of fact I want to compress all text files on a webserver, so that these are served compressen on Accept-Encoding. As a matter of fact, an uncompressed file of 41 bytes is compressed to 46 bytes. when the output is bigger than the input, the output file shall not be created by default.
claunia added the featurerelease-v1.1 labels 2026-01-29 20:41:57 +00:00
Author
Owner

@eustas commented on GitHub (Apr 23, 2020):

That is why I do the following thing:

cat << EOF > "$SITE/compress.sh"
#!/bin/sh
FILE=\$1
case "\${FILE##*.}" in
  "png") exit 0;;
  "jpg") exit 0;;
  "j") exit 0;;
esac
ORIG_SIZE=\`stat --printf="%s" \$FILE\`
brotli -Zfk \$FILE
BR_SIZE=\`stat --printf="%s" \$FILE.br\`
zopfli --i500 \$FILE
GZ_SIZE=\`stat --printf="%s" \$FILE.gz\`
if [ \$BR_SIZE -ge \$ORIG_SIZE ]; then rm \$FILE.br; fi
if [ \$GZ_SIZE -ge \$ORIG_SIZE ]; then rm \$FILE.gz; fi
EOF
chmod +x "$SITE/compress.sh"

rm $SITE/www/*.br
rm $SITE/www/*.gz
ls $SITE/www/* > "$SITE/www.files"
cat "$SITE/www.files" | xargs -n 1 -P 2 "$SITE/compress.sh"

I.e. remove files that become bigger.

@eustas commented on GitHub (Apr 23, 2020): That is why I do the following thing: ```bash cat << EOF > "$SITE/compress.sh" #!/bin/sh FILE=\$1 case "\${FILE##*.}" in "png") exit 0;; "jpg") exit 0;; "j") exit 0;; esac ORIG_SIZE=\`stat --printf="%s" \$FILE\` brotli -Zfk \$FILE BR_SIZE=\`stat --printf="%s" \$FILE.br\` zopfli --i500 \$FILE GZ_SIZE=\`stat --printf="%s" \$FILE.gz\` if [ \$BR_SIZE -ge \$ORIG_SIZE ]; then rm \$FILE.br; fi if [ \$GZ_SIZE -ge \$ORIG_SIZE ]; then rm \$FILE.gz; fi EOF chmod +x "$SITE/compress.sh" rm $SITE/www/*.br rm $SITE/www/*.gz ls $SITE/www/* > "$SITE/www.files" cat "$SITE/www.files" | xargs -n 1 -P 2 "$SITE/compress.sh" ``` I.e. remove files that become bigger.
Author
Owner

@dilyanpalauzov commented on GitHub (Apr 23, 2020):

It would be just better, if the brotli binary implictly does not create output files, that are bigger than the input. Not everybody is smart enough to clean afterwards the inversed compression.

@dilyanpalauzov commented on GitHub (Apr 23, 2020): It would be just better, if the brotli binary implictly does not create output files, that are bigger than the input. Not everybody is smart enough to clean afterwards the inversed compression.
Author
Owner

@eustas commented on GitHub (Apr 23, 2020):

It is easy to do, but that should not be the default behaviour (e.g. remain consistent with other compressors). Please consider filing the issue in google/brotli repo.

@eustas commented on GitHub (Apr 23, 2020): It is easy to do, but that should not be the default behaviour (e.g. remain consistent with other compressors). Please consider filing the issue in google/brotli repo.
Author
Owner

@dilyanpalauzov commented on GitHub (Apr 23, 2020):

Please consider filing the issue in google/brotli repo.

Isn't alreasy there?

If it shall not be the default behaviour then add a command line parameter for this.

@dilyanpalauzov commented on GitHub (Apr 23, 2020): > Please consider filing the issue in google/brotli repo. Isn't alreasy there? If it shall not be the default behaviour then add a command line parameter for this.
Author
Owner

@eustas commented on GitHub (Jul 2, 2020):

Sorry, was busy with different project and has responded with nonsense. Yup, you are at right place. And yes, this should be a command line parameter.
Though it will rather remove the output file, if it is bigger than input... Otherwise it would require working with temporary file, and thus might lead to non-trivial corner cases.
Still not sure, when I will have time to do it...

@eustas commented on GitHub (Jul 2, 2020): Sorry, was busy with different project and has responded with nonsense. Yup, you are at right place. And yes, this should be a command line parameter. Though it will rather remove the output file, if it is bigger than input... Otherwise it would require working with temporary file, and thus might lead to non-trivial corner cases. Still not sure, when I will have time to do it...
Author
Owner

@eustas commented on GitHub (Aug 3, 2023):

Should be fixed with #1036

@eustas commented on GitHub (Aug 3, 2023): Should be fixed with #1036
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/brotli#319