diff --git a/BUILD b/BUILD
index 1690d4219..e13ca8338 100644
--- a/BUILD
+++ b/BUILD
@@ -19,7 +19,7 @@ exports_files(["LICENSE"])
 # ZLIB configuration
 ################################################################################

-ZLIB_DEPS = ["@zlib//:zlib"]
+ZLIB_DEPS = ["@zlib"]

 ################################################################################
 # Protobuf Runtime Library
@@ -197,6 +197,7 @@ cc_library(
     includes = ["src/"],
     linkopts = LINK_OPTS,
     visibility = ["//visibility:public"],
+    alwayslink = 1,
 )

 PROTOBUF_DEPS = select({
@@ -271,6 +272,7 @@ cc_library(
     linkopts = LINK_OPTS,
     visibility = ["//visibility:public"],
     deps = [":protobuf_lite"] + PROTOBUF_DEPS,
+    alwayslink = 1,
 )

 # This provides just the header files for use in projects that need to build
diff --git a/src/google/protobuf/io/gzip_stream.h b/src/google/protobuf/io/gzip_stream.h
index f0283e86f..436c6ce4b 100644
--- a/src/google/protobuf/io/gzip_stream.h
+++ b/src/google/protobuf/io/gzip_stream.h
@@ -47,10 +47,13 @@
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/io/zero_copy_stream.h>
 #include <google/protobuf/port.h>
-#include <zlib.h>

 #include <google/protobuf/port_def.inc>

+#if HAVE_ZLIB
+#include <zlib.h>
+#endif  // HAVE_ZLIB
+
 namespace google {
 namespace protobuf {
 namespace io {
@@ -76,8 +79,10 @@ class PROTOBUF_EXPORT GzipInputStream : public ZeroCopyInputStream {
   virtual ~GzipInputStream();

   // Return last error message or NULL if no error.
+  #if HAVE_ZLIB
   inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
   inline int ZlibErrorCode() const { return zerror_; }
+  #endif  // HAVE_ZLIB

   // implements ZeroCopyInputStream ----------------------------------
   bool Next(const void** data, int* size) override;
@@ -90,8 +95,10 @@ class PROTOBUF_EXPORT GzipInputStream : public ZeroCopyInputStream {

   ZeroCopyInputStream* sub_stream_;

+  #if HAVE_ZLIB
   z_stream zcontext_;
   int zerror_;
+  #endif  // HAVE_ZLIB

   void* output_buffer_;
   void* output_position_;
@@ -143,8 +150,10 @@ class PROTOBUF_EXPORT GzipOutputStream : public ZeroCopyOutputStream {
   virtual ~GzipOutputStream();

   // Return last error message or NULL if no error.
+  #if HAVE_ZLIB
   inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
   inline int ZlibErrorCode() const { return zerror_; }
+  #endif  // HAVE_ZLIB

   // Flushes data written so far to zipped data in the underlying stream.
   // It is the caller's responsibility to flush the underlying stream if
@@ -177,8 +186,10 @@ class PROTOBUF_EXPORT GzipOutputStream : public ZeroCopyOutputStream {
   void* sub_data_;
   int sub_data_size_;

+  #if HAVE_ZLIB
   z_stream zcontext_;
   int zerror_;
+  #endif // HAVE_ZLIB
   void* input_buffer_;
   size_t input_buffer_length_;

