geekcomputers / geekcomputers/Python

Python-Copying a file into a folder present in a A.jar file without extracting

Open
#294 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
35.4k
Forks
12.9k
Avg merge
2h 37m
Merged PRs (30d)
1

Description

Hi ,
I was trying to copy a file into a A.jar (without extracting it) but it didn't work.
Suppose i have file copy.txt in D:\python\copy.txt and i want this file to be copied into my A.jar/org/here

for this I have written a code in python but what it is doing is , It is taking the contents of copy.txt and pasting the content to target dir-> A.jar/org/. It is not pasting the file instead it is pasting the content. I want the file to be copied with the content and if the file is already exist it will replace it in the target dir.
Please see my below code and suggest .

import zipfile, os

def show_jar_classes(jar_file):

zf = zipfile.ZipFile(jar_file, 'a')
try:
    #Code to write inside org folder in inside z.jar
    
    dir=input("please give the source file dir along with the file")# taking complete path frm user
    m='/'
    
    dir= dir.replace('\\',m) #formating the path by replacing '\' with '/'
    clas=os.path.basename(dir) #extracting the file name from the given path
    
    zf.write(dir, "org\\"+clas) #writing into A.jar/org/
finally:
    zf.close()

jar_file = 'D:/python/A.jar'
show_jar_classes(jar_file)

Extract to get A.jar.zip

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with the provided script, A.jar, and copy.txt, then inspect the zipfile.ZipFile call and the zf.write entry name. Confirm whether the archive contains the intended file entry under org and whether an existing entry is replaced; the issue is complete when this behavior is demonstrated reliably.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.