How to add url file to zip
<p>I have a zip file with the following structure:</p>

<pre><code>my_zip.zip
|-file1.txt
|-folder1/
    |-file2.txt
</code></pre>

<p>I want to add <code>some_file</code> <strong>from url</strong> to the <code>folder1</code>. I know that I can do something like:</p>

<pre><code>&gt;&gt;&gt; import zipfile
&gt;&gt;&gt; z = zipfile.ZipFile("my_zip.zip", "w")
&gt;&gt;&gt; z.write("some_file")
</code></pre>

<p>But, there are two issues:</p>

<ul>
<li>How to add the <code>some_file</code> to that specific <code>folder1</code>?</li>
<li>Should I download the <code>some_file</code> to my PC and next use <code>z.write('path/to_my/local/some_file')</code>? There is no way to do it directly from url to the zip?</li>
</ul>

<p>Use <a href="https://docs.python.org/3.7/library/zipfile.html#zipfile.ZipFile.writestr" rel="nofollow"><code>ZipFile.writestr(<em>arcname</em>, <em>data</em>)</code></a>.</p>

<p>To write to a folder in the zipfile, you just write the foldername as if you were writing to a folder in a folder (So <code>folder1/some_file</code>).</p>

<pre><code>import urllib.request
import zipfile

z = zipfile.ZipFile("my_zip.zip", "w")
page = urllib.request.urlopen('http://example.com/')  # Change to website
z.writestr('folder1/some_file', page.read())
</code></pre>

python python-3.x url zip