<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sam Rodrigues</title>
    <description>The latest articles on DEV Community by Sam Rodrigues (@samrodrigues).</description>
    <link>https://dev.to/samrodrigues</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4278627%2Fc627a024-fe86-41ef-8cec-6d30ee98ec4c.jpg</url>
      <title>DEV Community: Sam Rodrigues</title>
      <link>https://dev.to/samrodrigues</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samrodrigues"/>
    <language>en</language>
    <item>
      <title>Parsing a WhatsApp chat export: two line formats, invisible characters, and ambiguous date</title>
      <dc:creator>Sam Rodrigues</dc:creator>
      <pubDate>Wed, 02 Dec 2026 16:24:27 +0000</pubDate>
      <link>https://dev.to/samrodrigues/parsing-a-whatsapp-chat-export-two-line-formats-invisible-characters-and-ambiguous-date-45m6</link>
      <guid>https://dev.to/samrodrigues/parsing-a-whatsapp-chat-export-two-line-formats-invisible-characters-and-ambiguous-date-45m6</guid>
      <description>&lt;p&gt;A WhatsApp chat export looks simple enough to parse: one message per line, a date, a name, a colon, and the text. Then you get your first real export from a phone in another country, and half the messages disappear—or every date in March gets parsed as another month.&lt;/p&gt;

&lt;p&gt;Here is how the format actually behaves in the wild, along with a small Python parser you can run yourself. Everything below shows up in any standard export.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the text is
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Export chat&lt;/strong&gt; outputs a &lt;code&gt;.zip&lt;/code&gt; (or a plain &lt;code&gt;.txt&lt;/code&gt; on some Android versions). Inside, you'll find:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;iPhone:&lt;/strong&gt; &lt;code&gt;_chat.txt&lt;/code&gt;, plus media files if you selected "With all media".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Android:&lt;/strong&gt; &lt;code&gt;WhatsApp Chat with &amp;lt;name&amp;gt;.txt&lt;/code&gt;, where "WhatsApp Chat with" is localized to the phone's language (&lt;code&gt;Conversa do WhatsApp com …&lt;/code&gt;, &lt;code&gt;WhatsApp-Chat mit …&lt;/code&gt;). Instead of matching the file name, look for the &lt;code&gt;.txt&lt;/code&gt; file that isn't a media attachment.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Two line formats
&lt;/h2&gt;

&lt;p&gt;Every message begins with a header line, formatted differently on each platform:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[05/03/2024, 14:02:11] Ana Souza: did you get the contract?      iPhone
05/03/2024, 14:02 - Ana Souza: did you get the contract?         Android
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;iPhone wraps the timestamp in brackets and includes seconds, while Android uses &lt;code&gt;-&lt;/code&gt; as a separator and usually omits seconds. Beyond that, almost everything varies by locale:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Date separators can be &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;.&lt;/code&gt;, or &lt;code&gt;-&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Years can be two or four digits.&lt;/li&gt;
&lt;li&gt;Times can be 24-hour or 12-hour (&lt;code&gt;AM&lt;/code&gt;/&lt;code&gt;PM&lt;/code&gt;, &lt;code&gt;a.m.&lt;/code&gt;/&lt;code&gt;p.m.&lt;/code&gt;, or localized equivalents).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Any line that does &lt;strong&gt;not&lt;/strong&gt; start with a header belongs to the previous message. Since multi-line messages are common, your parser needs to append continuation lines to the last message rather than treating each line as standalone.&lt;/p&gt;

&lt;h2&gt;
  
  
  The invisible characters
&lt;/h2&gt;

&lt;p&gt;Two invisible Unicode characters will break a basic regex:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;U+200E (LEFT-TO-RIGHT MARK):&lt;/strong&gt; iPhone exports insert this at the start of system notices and attachment lines, which causes &lt;code&gt;line.startswith("[")&lt;/code&gt; to return false.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;U+202F (NARROW NO-BREAK SPACE):&lt;/strong&gt; Newer iPhones place this between the time and &lt;code&gt;PM&lt;/code&gt; (&lt;code&gt;2:02 PM&lt;/code&gt; is actually &lt;code&gt;2:02 PM&lt;/code&gt;), so a literal space in your pattern won't match.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Strip U+200E before matching, and use &lt;code&gt;\s&lt;/code&gt; (which covers U+202F in Python's &lt;code&gt;re&lt;/code&gt;) before the AM/PM marker.&lt;/p&gt;

&lt;h2&gt;
  
  
  A parser in 30 lines
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;

&lt;span class="no"&gt;HEADER&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"^\[?(?P&amp;lt;date&amp;gt;\d{1,4}[./-]\d{1,2}[./-]\d{1,4}),?\s"&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"(?P&amp;lt;time&amp;gt;\d{1,2}[:.]\d{2}(?:[:.]\d{2})?(?:\s?[APap]\.?\s?[Mm]\.?)?)\]?"&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"(?:\s-\s|\s)"&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"(?P&amp;lt;rest&amp;gt;.*)$"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splitlines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"‎"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;HEADER&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s2"&gt;"\n"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sep&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"rest"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;": "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"date"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"date"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"time"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"time"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"sender"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;sep&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# None: a notice, not a person&lt;/span&gt;
            &lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;sep&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"rest"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A couple of details make this hold up in practice. Using &lt;code&gt;partition(": ")&lt;/code&gt; splits only on the &lt;strong&gt;first&lt;/strong&gt; colon-space, so messages containing &lt;code&gt;:&lt;/code&gt; stay intact. Also, a header without a &lt;code&gt;Name:&lt;/code&gt; prefix is a WhatsApp system notice ("Messages and calls are end-to-end encrypted", "Ana added Bruno"), so setting &lt;code&gt;sender = None&lt;/code&gt; keeps it separate from actual participants.&lt;/p&gt;

&lt;h2&gt;
  
  
  Day first or month first?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;05/03/2024&lt;/code&gt; means 5 March in Brazil and 3 May in the US, and the export never states which format it uses. You can't reliably infer it from the phone's language or the clock format either—India, the UK, and Australia all use day-first dates with a 12-hour clock.&lt;/p&gt;

&lt;p&gt;Instead, scan the dates in the chat: if the first number is over 12, it's day-first; if the second number is over 12, it's month-first. A single unambiguous date is enough for the whole file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;date_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"[./-]"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"date"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"ymd"&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"dmy"&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"mdy"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;  &lt;span class="c1"&gt;# every date fits both readings: ask the user&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this returns &lt;code&gt;None&lt;/code&gt; (for example, a short chat from the first 12 days of a month), there is no way to know for sure. Fall back to a default and let the user know you assumed it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attachments
&lt;/h2&gt;

&lt;p&gt;When media is included, attachment messages reference the file name directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[05/03/2024, 14:03:00] Ana Souza: &amp;lt;attached: 00000012-PHOTO-2024-03-05-14-03-00.jpg&amp;gt;    iPhone
05/03/2024, 14:03 - Ana Souza: IMG-20240305-WA0003.jpg (file attached)                  Android
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The surrounding labels are translated (&lt;code&gt;&amp;lt;anexado: …&amp;gt;&lt;/code&gt;, &lt;code&gt;(Datei angehängt)&lt;/code&gt;), so maintaining a list of localized phrases doesn't scale. The structure, however, stays consistent: angle brackets around &lt;code&gt;label: filename.ext&lt;/code&gt; on iPhone, and &lt;code&gt;filename.ext (label)&lt;/code&gt; on Android. Match the pattern and file extension rather than the text.&lt;/p&gt;

&lt;p&gt;Voice messages follow &lt;code&gt;PTT-….opus&lt;/code&gt; on Android and &lt;code&gt;…-AUDIO-….opus&lt;/code&gt; on iPhone. If the export was created without media, the line only contains a localized placeholder (&lt;code&gt;&amp;lt;Media omitted&amp;gt;&lt;/code&gt;, &lt;code&gt;image omitted&lt;/code&gt;) and the file isn't included.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you'd rather not write it
&lt;/h2&gt;

&lt;p&gt;I packaged this up under the MIT license as &lt;a href="https://github.com/samrodrigues/whatsapp-export-reader" rel="noopener noreferrer"&gt;&lt;code&gt;whatsapp-export-reader&lt;/code&gt;&lt;/a&gt;. It takes a &lt;code&gt;.zip&lt;/code&gt; or &lt;code&gt;.txt&lt;/code&gt; and outputs messages with linked attachments, or exports to Markdown, JSON, or CSV:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx whatsapp-export-reader &lt;span class="s2"&gt;"WhatsApp Chat with Ana.zip"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; chat.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is also an &lt;a href="https://github.com/samrodrigues/whatsapp-export-mcp" rel="noopener noreferrer"&gt;MCP server&lt;/a&gt; and a &lt;a href="https://github.com/samrodrigues/whatsapp-export-skills" rel="noopener noreferrer"&gt;Claude skill&lt;/a&gt; built on top of it if you want to query a chat with an AI assistant.&lt;/p&gt;

&lt;p&gt;I build &lt;a href="https://zap2doc.com/" rel="noopener noreferrer"&gt;Zap2Doc&lt;/a&gt;, which converts these exports into PDFs with transcribed voice messages, and getting this parsing layer right is step one. If you missed the previous post, it covers what happens when exports get re-zipped on Windows: &lt;a href="https://dev.to/samrodrigues/the-whatsapp-export-your-zip-library-cant-open-deflate64-legacy-file-names-and-a-zip-inside-a-zip-3ipm"&gt;The WhatsApp export your zip library can't open&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>regex</category>
      <category>opensource</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The WhatsApp export your zip library can't open: Deflate64, legacy file names and a zip inside a zip</title>
      <dc:creator>Sam Rodrigues</dc:creator>
      <pubDate>Wed, 02 Dec 2026 13:36:18 +0000</pubDate>
      <link>https://dev.to/samrodrigues/the-whatsapp-export-your-zip-library-cant-open-deflate64-legacy-file-names-and-a-zip-inside-a-zip-3ipm</link>
      <guid>https://dev.to/samrodrigues/the-whatsapp-export-your-zip-library-cant-open-deflate64-legacy-file-names-and-a-zip-inside-a-zip-3ipm</guid>
      <description>&lt;p&gt;If you build anything that processes WhatsApp chat exports, you will eventually get an upload that your zip library rejects, even though it opens without issue on the user's computer. The original export is usually fine—the problem is what Windows does to the file before it reaches your server.&lt;/p&gt;

&lt;p&gt;Below are the three issues we run into most often, how to catch them by reading only the archive's central directory, and how to fix them before the rest of your pipeline touches the file.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Deflate64: "compression type 9"
&lt;/h2&gt;

&lt;p&gt;WhatsApp generates a standard zip. However, users frequently extract it to check the contents, then re-compress it using Windows Explorer's "Send to → Compressed (zipped) folder". For larger archives, Explorer uses &lt;strong&gt;Deflate64&lt;/strong&gt; (method 9), a Deflate variant with a 64 KB window.&lt;/p&gt;

&lt;p&gt;Most standard zip libraries don't support it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python's &lt;code&gt;zipfile&lt;/code&gt; throws &lt;code&gt;NotImplementedError: compression type 9 (deflate64)&lt;/code&gt; when reading an entry.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;node-stream-zip&lt;/code&gt; fails with &lt;code&gt;Unknown compression method: 9&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Listing the archive still works because the central directory is intact; it only fails when you try to read the entries. Check the compression method up front so you don't crash midway through processing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;zipfile&lt;/span&gt;

&lt;span class="no"&gt;DEFLATE64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;needs_rewrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;zipfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ZipFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compress_type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;DEFLATE64&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;infolist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To read these entries anyway:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Python:&lt;/strong&gt; &lt;a href="https://pypi.org/project/zipfile-deflate64/" rel="noopener noreferrer"&gt;&lt;code&gt;zipfile-deflate64&lt;/code&gt;&lt;/a&gt; patches &lt;code&gt;zipfile&lt;/code&gt; so the standard API can extract method 9.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Node:&lt;/strong&gt; &lt;a href="https://www.npmjs.com/package/zlib-streams" rel="noopener noreferrer"&gt;&lt;code&gt;zlib-streams&lt;/code&gt;&lt;/a&gt; includes a WASM inflater that supports Deflate64.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of adding Deflate64 support to every service in your pipeline, rewrite the archive once right after upload using standard storage or Deflate (method 0 or 8) and replace the original. Downstream code gets a regular zip, and you only deal with the format quirk in one place.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Non-UTF-8 file names
&lt;/h2&gt;

&lt;p&gt;The zip spec uses bit 11 of the general-purpose flags to indicate that a file name is UTF-8. When that bit is off, the spec defaults to code page 437. Windows Explorer writes file names using the machine's legacy code page and leaves the flag off. As a result, a voice note or photo containing an accented contact name ends up with bytes that are neither flagged as UTF-8 nor reliably CP437.&lt;/p&gt;

&lt;p&gt;This breaks WhatsApp parsing because the chat text references attachments by name (&lt;code&gt;PTT-20240518-WA0007.opus (file attached)&lt;/code&gt;, or a photo named after the contact). If the file name in the archive decodes differently from the text in the chat, the match fails silently and the message loses its media.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Python&lt;/strong&gt; decodes unflagged names as CP437, so it won't crash, though characters can come out mangled.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Some Node libraries&lt;/strong&gt; decode them as UTF-8 and produce U+FFFD replacement characters, which won't match anything.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To handle this reliably, if the UTF-8 flag is off, grab the raw bytes, try decoding as UTF-8 first (many tools write UTF-8 without setting the flag), and fall back to CP437. In Python, you can recover the raw bytes from the CP437 string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;UTF8_FLAG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x800&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;real_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;zipfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ZipInfo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flag_bits&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="no"&gt;UTF8_FLAG&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;
    &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cp437"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;UnicodeDecodeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the rewritten archive with UTF-8 names and the flag enabled so every downstream reader sees the exact same file names.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. A zip inside a zip
&lt;/h2&gt;

&lt;p&gt;Windows hides known file extensions by default. A user sees &lt;code&gt;WhatsApp Chat with Ana&lt;/code&gt; (which is actually &lt;code&gt;WhatsApp Chat with Ana.zip&lt;/code&gt;), right-clicks it, and compresses it a second time. Your server ends up with an archive containing a single entry and no &lt;code&gt;_chat.txt&lt;/code&gt;. That single entry is the real export.&lt;/p&gt;

&lt;p&gt;If your validation just tells them "this is not a WhatsApp export, use Export chat", you're asking them to repeat what they already did. They'll usually re-upload the same file and give up.&lt;/p&gt;

&lt;p&gt;It's easy to catch: there is only one entry, no chat text at the root, and the entry starts with the zip signature &lt;code&gt;PK\x03\x04&lt;/code&gt;. Extract that inner archive (applying the same size limits as any other entry) and pass it through the same repair logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cap every read twice
&lt;/h2&gt;

&lt;p&gt;All three fixes involve inflating untrusted archives. You can't rely on the uncompressed size listed in the central directory, since it's just metadata. Instead:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reject any entry whose &lt;strong&gt;declared&lt;/strong&gt; size exceeds your limit. This happens before any inflation, so it's cheap.&lt;/li&gt;
&lt;li&gt;Track the bytes as you inflate, and abort if the &lt;strong&gt;actual total&lt;/strong&gt; exceeds either the declared size or your hard limit.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you only check the first one, you're trusting whatever number the uploader wrote. A highly compressible file—under a megabyte on disk, hundreds of megabytes inflated—can push a worker past a gigabyte of RAM in less than a second.&lt;/p&gt;

&lt;p&gt;If a single entry fails a size check or CRC validation, skip it and keep processing the rest instead of failing the entire upload. One corrupted photo shouldn't block the rest of the conversation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling it in one place
&lt;/h2&gt;

&lt;p&gt;All three issues stem from the OS modifying the file between WhatsApp and your server, and the cleanest fix is a single normalization step on upload. Inspect the central directory, check if a rewrite is needed, re-pack it once with standard compression and UTF-8 names, and pass a predictable archive to the rest of your pipeline.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;We ran into these cases while turning WhatsApp exports into PDFs with transcribed voice messages at &lt;a href="https://zap2doc.com/" rel="noopener noreferrer"&gt;Zap2Doc&lt;/a&gt;. If you're building your own parser, these three checks will handle most of the "it opens fine on my computer" uploads.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>node</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
