--- tclZipfs_original.c	Sun Nov 24 15:41:53 2024
+++ tclZipfs.c	Sun Nov 24 20:04:46 2024
@@ -336,11 +336,12 @@
 static void		SerializeCentralDirectoryEntry(
 			    const unsigned char *start,
 			    const unsigned char *end, unsigned char *buf,
-			    ZipEntry *z, size_t nameLength);
+			    ZipEntry *z, size_t nameLength, long long dataStartOffset);
 static void		SerializeCentralDirectorySuffix(
 			    const unsigned char *start,
 			    const unsigned char *end, unsigned char *buf,
-			    int entryCount, long long directoryStartOffset,
+			    int entryCount, long long dataStartOffset,
+                long long directoryStartOffset,
 			    long long suffixStartOffset);
 static void		SerializeLocalEntryHeader(
 			    const unsigned char *start,
@@ -3355,6 +3356,8 @@
     int count, ret = TCL_ERROR;
     Tcl_Size pwlen = 0, slen = 0, len, i = 0;
     Tcl_Size lobjc;
+    long long dataStartOffset; /* The overall file offset of the start of the
+				 * data section of the file. */
     long long directoryStartOffset;
     /* The overall file offset of the start of the
      * central directory. */
@@ -3527,6 +3530,9 @@
 	}
 	memset(passBuf, 0, sizeof(passBuf));
 	Tcl_Flush(out);
+	dataStartOffset = Tcl_Tell(out);
+    } else {
+	dataStartOffset = 0;
     }
 
     /*
@@ -3578,7 +3584,7 @@
 	name = Tcl_DStringValue(&ds);
 	len = Tcl_DStringLength(&ds);
 	SerializeCentralDirectoryEntry(start, end, (unsigned char *) buf,
-		z, len);
+		z, len, dataStartOffset);
 	if ((Tcl_Write(out, buf, ZIP_CENTRAL_HEADER_LEN)
 		!= ZIP_CENTRAL_HEADER_LEN)
 		|| (Tcl_Write(out, name, len) != len)) {
@@ -3598,7 +3604,7 @@
     Tcl_Flush(out);
     suffixStartOffset = Tcl_Tell(out);
     SerializeCentralDirectorySuffix(start, end, (unsigned char *) buf,
-	    count, directoryStartOffset, suffixStartOffset);
+	    count, dataStartOffset, directoryStartOffset, suffixStartOffset);
     if (Tcl_Write(out, buf, ZIP_CENTRAL_END_LEN) != ZIP_CENTRAL_END_LEN) {
 	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
 		"write error: %s", Tcl_PosixError(interp)));
@@ -3755,7 +3761,9 @@
     const unsigned char *end,	/* The end of writable memory. */
     unsigned char *buf,		/* Where to serialize to */
     ZipEntry *z,		/* The description of what to serialize. */
-    size_t nameLength)		/* The length of the name. */
+    size_t nameLength,		/* The length of the name. */
+    long long dataStartOffset)	/* The overall file offset of the start of the 
+    				 * data section of the file. */
 {
     ZipWriteInt(start, end, buf + ZIP_CENTRAL_SIG_OFFS,
 	    ZIP_CENTRAL_HEADER_SIG);
@@ -3780,7 +3788,7 @@
     ZipWriteShort(start, end, buf + ZIP_CENTRAL_IATTR_OFFS, 0);
     ZipWriteInt(start, end, buf + ZIP_CENTRAL_EATTR_OFFS, 0);
     ZipWriteInt(start, end, buf + ZIP_CENTRAL_LOCALHDR_OFFS,
-	    z->offset);
+	    z->offset - dataStartOffset);
 }
 
 static void
@@ -3789,6 +3797,9 @@
     const unsigned char *end,	/* The end of writable memory. */
     unsigned char *buf,		/* Where to serialize to */
     int entryCount,		/* The number of entries in the directory */
+    long long dataStartOffset,
+				/* The overall file offset of the start of the
+				 * data file. */
     long long directoryStartOffset,
 				/* The overall file offset of the start of the
 				 * central directory. */
@@ -3805,7 +3816,7 @@
     ZipWriteInt(start, end, buf + ZIP_CENTRAL_DIRSIZE_OFFS,
 	    suffixStartOffset - directoryStartOffset);
     ZipWriteInt(start, end, buf + ZIP_CENTRAL_DIRSTART_OFFS,
-	    directoryStartOffset);
+	    directoryStartOffset - dataStartOffset);
     ZipWriteShort(start, end, buf + ZIP_CENTRAL_COMMENTLEN_OFFS, 0);
 }
 
