{"id":4846,"date":"2024-03-14T02:01:04","date_gmt":"2024-03-14T02:01:04","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/"},"modified":"2025-07-31T14:17:39","modified_gmt":"2025-07-31T14:17:39","slug":"how-to-preserve-the-source-file-when-compressing-with-gzip-in-java","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/","title":{"rendered":"Java Gzip: Keep Original File"},"content":{"rendered":"<p>In Java, you can use GZIPOutputStream to perform gzip compression, but you cannot directly retain the original file because the compression operation processes the data and outputs it to a target file. If you want to retain the original file, you can first read the content of the source file into memory, then perform the compression operation, and finally write the compressed data to the target file and write the original file content back to the source file.<\/p>\n<p>Here is a simple sample code demonstrating how to compress using gzip and keep the original file.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">import<\/span> java.io.*;\r\n<span class=\"hljs-keyword\">import<\/span> java.util.zip.GZIPOutputStream;\r\n\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">GzipExample<\/span> {\r\n\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">main<\/span><span class=\"hljs-params\">(String[] args)<\/span> {\r\n        <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">sourceFile<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"source.txt\"<\/span>;\r\n        <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">targetFile<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"target.gz\"<\/span>;\r\n\r\n        <span class=\"hljs-keyword\">try<\/span> {\r\n            <span class=\"hljs-comment\">\/\/ \u8bfb\u53d6\u6e90\u6587\u4ef6\u5185\u5bb9<\/span>\r\n            <span class=\"hljs-type\">FileInputStream<\/span> <span class=\"hljs-variable\">fis<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">FileInputStream<\/span>(sourceFile);\r\n            <span class=\"hljs-type\">ByteArrayOutputStream<\/span> <span class=\"hljs-variable\">baos<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">ByteArrayOutputStream<\/span>();\r\n            <span class=\"hljs-type\">byte<\/span>[] buffer = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">byte<\/span>[<span class=\"hljs-number\">1024<\/span>];\r\n            <span class=\"hljs-type\">int<\/span> bytesRead;\r\n            <span class=\"hljs-keyword\">while<\/span> ((bytesRead = fis.read(buffer)) != -<span class=\"hljs-number\">1<\/span>) {\r\n                baos.write(buffer, <span class=\"hljs-number\">0<\/span>, bytesRead);\r\n            }\r\n\r\n            <span class=\"hljs-comment\">\/\/ \u538b\u7f29\u6e90\u6587\u4ef6\u5185\u5bb9<\/span>\r\n            <span class=\"hljs-type\">FileOutputStream<\/span> <span class=\"hljs-variable\">fos<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">FileOutputStream<\/span>(targetFile);\r\n            <span class=\"hljs-type\">GZIPOutputStream<\/span> <span class=\"hljs-variable\">gos<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">GZIPOutputStream<\/span>(fos);\r\n            gos.write(baos.toByteArray());\r\n            gos.close();\r\n\r\n            <span class=\"hljs-comment\">\/\/ \u5c06\u6e90\u6587\u4ef6\u5185\u5bb9\u5199\u56de\u6e90\u6587\u4ef6<\/span>\r\n            <span class=\"hljs-type\">FileOutputStream<\/span> <span class=\"hljs-variable\">fos2<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">FileOutputStream<\/span>(sourceFile);\r\n            fos2.write(baos.toByteArray());\r\n\r\n            fis.close();\r\n            baos.close();\r\n            fos.close();\r\n            fos2.close();\r\n            \r\n            System.out.println(<span class=\"hljs-string\">\"\u538b\u7f29\u5e76\u4fdd\u7559\u6e90\u6587\u4ef6\u6210\u529f\"<\/span>);\r\n        } <span class=\"hljs-keyword\">catch<\/span> (IOException e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>In this example, the source file content is first read into memory, then compressed using GZIPOutputStream, and the compressed data is written to the target file. Finally, the source file content is written back to the source file, completing the compression while preserving the original file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Java, you can use GZIPOutputStream to perform gzip compression, but you cannot directly retain the original file because the compression operation processes the data and outputs it to a target file. If you want to retain the original file, you can first read the content of the source file into memory, then perform the [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[1814,4300,4794,87,4793],"class_list":["post-4846","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-file-compression","tag-gzip","tag-gzipoutputstream","tag-java","tag-source-file-preservation"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Gzip: Keep Original File - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how to preserve source files when using Gzip compression in Java. Step-by-step guide with code examples for file retention during compression.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Gzip: Keep Original File\" \/>\n<meta property=\"og:description\" content=\"Learn how to preserve source files when using Gzip compression in Java. Step-by-step guide with code examples for file retention during compression.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-14T02:01:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-31T14:17:39+00:00\" \/>\n<meta name=\"author\" content=\"Emily Johnson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Emily Johnson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/\"},\"author\":{\"name\":\"Emily Johnson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\"},\"headline\":\"Java Gzip: Keep Original File\",\"datePublished\":\"2024-03-14T02:01:04+00:00\",\"dateModified\":\"2025-07-31T14:17:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/\"},\"wordCount\":146,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"File compression\",\"Gzip\",\"GZIPOutputStream\",\"Java\",\"Source file preservation\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/\",\"name\":\"Java Gzip: Keep Original File - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-14T02:01:04+00:00\",\"dateModified\":\"2025-07-31T14:17:39+00:00\",\"description\":\"Learn how to preserve source files when using Gzip compression in Java. Step-by-step guide with code examples for file retention during compression.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Gzip: Keep Original File\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\",\"name\":\"Emily Johnson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"caption\":\"Emily Johnson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java Gzip: Keep Original File - Blog - Silicon Cloud","description":"Learn how to preserve source files when using Gzip compression in Java. Step-by-step guide with code examples for file retention during compression.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Java Gzip: Keep Original File","og_description":"Learn how to preserve source files when using Gzip compression in Java. Step-by-step guide with code examples for file retention during compression.","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-14T02:01:04+00:00","article_modified_time":"2025-07-31T14:17:39+00:00","author":"Emily Johnson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Emily Johnson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/"},"author":{"name":"Emily Johnson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378"},"headline":"Java Gzip: Keep Original File","datePublished":"2024-03-14T02:01:04+00:00","dateModified":"2025-07-31T14:17:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/"},"wordCount":146,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["File compression","Gzip","GZIPOutputStream","Java","Source file preservation"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/","name":"Java Gzip: Keep Original File - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-14T02:01:04+00:00","dateModified":"2025-07-31T14:17:39+00:00","description":"Learn how to preserve source files when using Gzip compression in Java. Step-by-step guide with code examples for file retention during compression.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-preserve-the-source-file-when-compressing-with-gzip-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java Gzip: Keep Original File"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378","name":"Emily Johnson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","caption":"Emily Johnson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/4846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=4846"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/4846\/revisions"}],"predecessor-version":[{"id":149558,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/4846\/revisions\/149558"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=4846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=4846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=4846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}