Htmlcompressor
HTML Compressor and Minifier, can be used standalone and as a Java library
Install / Use
/learn @serg472/HtmlcompressorREADME
!!! This library is no longer maintained !!!
HtmlCompressor is a small, fast and very easy to use Java library that minifies given HTML or XML source by removing extra whitespaces, comments and other unneeded characters without breaking the content structure. As a result pages become smaller in size and load faster. A command-line version of the compressor is also available.
How it Works
During HTML compression the following is applied to the page source:
- Any content within
<pre>,<textarea>,<script>and<style>tags will be preserved and remain untouched (with the exception of<script type="text/x-jquery-tmpl">tags which are compressed as HTML). Inline javascript inside tags (onclick="test()") will be preserved as well. You can wrap any part of the page in<!-- {{{ -->...<!-- }}} -->comments to preserve it, or provide a set of your own preservation rules (out of the box<?php...?>,<%...%>, and<!--#... -->are also supported) - Comments are removed (except IE conditional comments). Could be disabled.
- Multiple spaces are replaced with a single space. Could be disabled.
- Unneeded spaces inside tags (around
=and before/>) are removed. - Quotes around tag attributes could be removed when safe (off by default).
- All spaces between tags could be removed (off by default).
- Spaces around selected tags could be removed (off by default).
- Existing doctype declaration could be replaced with simple
<!DOCTYPE html>declaration (off by default). - Default attributes from
<script>,<style>,<link>,<form>,<input>tags could be removed (off by default). - Values from boolean tag attributes could be removed (off by default).
javascript:pseudo-protocol could be removed from inline event handlers (off by default).http://andhttps://protocols could be replaced with//insidehref,src,cite, andactiontag attributes (tags marked withrel="external"are skipped).- Content inside
<style>tags could be optionally compressed using YUI compressor or your own compressor implementation. - Content inside
<script>could be optionally compressed using YUI compressor, Google Closure Compiler or your own compressor implementation.
With default settings your compressed HTML layout should be 100% identical to the original in all browsers (only characters that are completely safe to remove are removed). Optional settings (that should be safe in 99% cases) would give you extra savings.
You can optionally remove all unnecessary quotes from tag attributes (attributes that consist from a single word: <div id="example"> would become <div id=example>). This usually gives around 3% pagesize decrease at no performance cost but might break strict HTML validation so this option is disabled by default.
About extra 3% pagesize can be saved by removing inter-tag spaces. It is fairly safe to turn this option on unless you rely on spaces for page formatting. Even if you do, you can always preserve required spaces with  or . This option has no performance impact.
You can quickly test how each of the compressor settings would affect filesize of your page by running command line HTML Analyzer.
During XML compression:
- Any content inside
<![CDATA[...]]>is preserved. - All comments are removed. Could be disabled.
- All spaces between tags are removed. Could be disabled.
- Unneeded spaces inside tags (multiple spaces, spaces around
=, spaces before/>) are removed.
How to Use
Before reading further, if you are not serving your HTML/XML content using GZip compression, you should really look into that first, as it would give you very significant compression ratio (sometimes up to 10 times) and usually very easy to implement. For further reading on GZip compression please see this article for example.
If you want to reach further size decrease, the next step would be removing insignificant, from browser's perspective, characters from your pages, that's where this library comes in handy.
Juriy Zaytsev did an excellent detailed research on HTML minification techniques, which you can use as a guide to what HTML compression settings would work best for your project. Please see his Optimizing HTML and Experimenting with html minifier articles.
A convenient web interface for the HTML compressor called HTML Compiler was created by Erich Reich where you can experiment with different compression settings and preview online how your page source is affected.
For Java Projects
If you are generating static HTML files on the server, the most flexible solution would be calling html compression before writing output to the file. If you are generating HTML files once in a while and then uploading them to a production server, the easiest solution that doesn't require any code modifications would be using ANT task that calls the command line version of the library and rewrites files with their compressed versions.
For dynamic sites that are using JSP, the best way of compressing the output would be using compressor taglib.
For dynamic sites using Velocity, you can either wrap your templates with compressor directives or call compressor manually after merging the template.
For other dynamic cases you will probably have to call compressors directly from the code before serving a page to the client.
HtmlCompressor and XmlCompressor classes are considered thread safe* and can be used in multi-thread environment (the only unsafe part is setting compression options, so it would be a good idea to initialize a compressor with required settings once per application and then use it to compress different pages in parallel in multiple threads. Please note that enabling statistics generation makes compressor not safe).
For Maven projects HtmlCompressor library is available as a Maven artifact.
For Non-Java Projects
If you are generating HTML for your site (or have simple site in pure HTML) you can use a command line version of the library (Java still must be installed). For dynamic sites it other languages the only option would be programmatically executing shell command that runs command line compressor.
Bad Practices
- Don't feed the compressor actual templates (php, jsp, etc). This most likely won't work, even if it does it would be a bad idea anyway as you will lose their readability and further development will be very inconvenient. Instead of compressing templates you should consider compressing resulting html after a template is merged. If you absolutely have to compress templates, you need to set custom block preservation rules for HTML Compressor.
- If your site is in pure HTML, always keep original files and only compress their copies that will be served to the client. If you compress your only sources, again your further development will be very hard and there is no easy way to decompress pages back.
Known Issues
- When
<script>tag contains some custom preserved block (for example<?php>), enabling inline javascript compression will fail. Such<script>tags could be skipped by wrapping them with<!-- {{{ -->...<!-- }}} -->comments (skip blocks). - Removing intertag spaces might break text formatting, for example spaces between words surrounded with
<b>will be removed. Such spaces might be preserved by replacing them withor .
Dependencies
XML compressor doesn't rely on any external libraries.
HTML compressor with default settings doesn't require any dependencies.
Inline CSS compression requires YUI compressor library.
Inline JavaScript compression requires either YUI compressor library (by default) or Google Closure Compiler library.
All dependencies could be found in /lib/ folder of the package.
Please note that if using command line compressor, there are strict restrictions on jar filenames for dependencies (more details in the command line compressor section below).
Compressing HTML and XML files from a command line
If you have Java installed, you can run HTML and XML compressors from a command line.
For inline JavaScript compression you can choose between YUI Compressor (default) and Google Closure Compiler. If YUI compressor is used, jar file yuicompressor-2.4.6.jar (or yuicompressor-2.4.*.jar or yuicompressor.jar) must be present at the same directory as HtmlCompressor jar. For Closure Compiler compiler.jar must be present. (Please note that jar filenames cannot be changed).
Usage: java -jar htmlcompressor.jar [options] [input]
[input] URL, filename, directory, or space separated list
of files and directories to compress.
If none provided reads from <stdin>
Global Options:
-?, /?, -h, --help Displays this help screen
-t, --type <html|xml> If not provided autodetects from file extension
-r, --recursive Process files inside subdirectories
-c, --charset <charset> Charset for reading files, UTF-8 by default
-m, --mask <filemask> Filter input files inside directories by mask
-o, --output <path> Filename or directory for compression results.
If none provided outputs result to <stdout>
-a, --analyze Tries different settings and displays report.
All settings except --js-compressor are ignored
XML Compressi
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
