File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Linux / UNIX and the fly likes FTP Put a zip file that contains text files in Linux Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "FTP Put a zip file that contains text files in Linux" Watch "FTP Put a zip file that contains text files in Linux" New topic

FTP Put a zip file that contains text files in Linux

kazeopeia joshi

Joined: Jul 07, 2011
Posts: 12
Hi all,

I am transferring a file from a linux pc going to a windows ftp server using "ftp put". My file is a zip file that includes .txt files inside it.

Here is what's happening when I transfered this file :
1. I used ftp put for transferring and found that my transferred zip file was corrupted and couldn't be opened on the ftp server.
2. I found the solution for this on the internet. I needed to use 'binary' to make it right.
3. I transferred again using binary and then ftp put the zip file onto the other end. Yes, it worked. My zip file wasn't corrupted anymore and I could already opened it on the ftp server. But the problem remains on the .txt files inside it. Converting the file into binary made my .txt files to be distorted and unreadable. I read from the internet that .txt files need to use Ascii instead of Binary to be readable, but if I use ascii it would cause my zip file to be corrupted again.

Is there any way to solve this? I need to successfully transfer a zip file that contains .txt files using ftp put.

Please help.
Thank you very much for any quick and kind response.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

FTP servers on Windows usually default to text/ascii mode for transfers. Linux FTP servers more often default to binary mode. When in doubt, set the transfer mode manually before transmitting/receiving files.

What text mode actually does is clean up the differences between DOS/Windows text files and Unix-format text files. For historical reasons, DOS text files terminate text lines with CR/LF and originally ended the file with a "control-Z" character. Unix figures that one end-of-line character is enough, so when uploading and storing in text mode, the CR and Control-Z characters are removed. Since CR is a hexadecimal 0D and ZIP files often have 0D bytes in their binary content simply because one byte value is about as likely as any other in binary, transmitting a ZIP file in text mode will generally damage it.

However, when you upload a ZIP file with Windows-generated text files in it, unzip it and attempt to use those files on a Linux or Unix system, those CR characters won't have been converted. Likewise, when a file is sent to Windows, the CR characters won't have been added. Which isn't a problem for Wordpad, but Windows Notepad wants those CR characters, as does the old DOS EDIT program.

To convert text files in a ZIP archive, you have at least 2 options:

1. Unzip utilities often have a "convert text" option. On the unZip utility that I use, it's "-a". This will convert the files as they are unzipped.

2. There is also a pair of programs called unix2dos/dos2unix that can take files and convert them.

3. (I did say AT LEAST 2! ) if you're a die-hard Unix geek, you can use a standard text utility such as the "tr" program to convert the files.

An IDE is no substitute for an Intelligent Developer.
kazeopeia joshi

Joined: Jul 07, 2011
Posts: 12
Thanks so much. I found the answer.

FTP Transfer of zip file from linux to windows server doesn't affect the contents of the zip file. I am generating my text files dynamically through java code and what actually happens is that text files transferred to windows ftp server uses the line ending \r\n instead of just plain newline ( \n ).

I just changed my line endings into \r\n to make it work and be readable when opened even on notepad (because I am really required to open the files using notepad).

Thank you so much for all your help ^.^
I agree. Here's the link:
subject: FTP Put a zip file that contains text files in Linux
It's not a secret anymore!