Java 解包压缩错误
所以当我试图在pc上解压我的zip文件时出现了错误 7zip错误-试图在文件开头之前移动文件指针 UPD-发送到ftp后,在发送存档文件之前发生错误强> 对于使用ftp,我使用ClientFTP库 首先,在我的设备上,我创建了一个带有gps坐标的文本文件,然后我尝试将其压缩并上传到ftp 然后下载到pc并解压缩 我尝试了许多方法来创建归档 下面的代码创建一个文本文件Java 解包压缩错误,java,android,zip,Java,Android,Zip,所以当我试图在pc上解压我的zip文件时出现了错误 7zip错误-试图在文件开头之前移动文件指针 UPD-发送到ftp后,在发送存档文件之前发生错误 对于使用ftp,我使用ClientFTP库 首先,在我的设备上,我创建了一个带有gps坐标的文本文件,然后我尝试将其压缩并上传到ftp 然后下载到pc并解压缩 我尝试了许多方法来创建归档 下面的代码创建一个文本文件 File file; try{ file = new File(path, "locations.lft");
File file;
try{
file = new File(path, "locations.lft");
if (file.exists())
file.delete();
file.createNewFile();
}catch (Exception e){
try{ftp.quit();}catch(Exception E){}
return;
}
BufferedWriter BW = null;
try{
BW = new BufferedWriter(new FileWriter(file));
}catch(Exception e){
try{ftp.quit();}catch(Exception E){}
return;
}
Cursor row = db.Query("locations",null,"date_time BETWEEN " + ToBeggingOfDay(date).getTime() + " AND " +
ToEndOfDay(date).getTime());
try{
while (row.moveToNext()){
String line = "" + row.getString(row.getColumnIndex("latitude")) + "~" + row.getString(row.getColumnIndex("longtitude")) + "~" + DateFormat.format("yyyy-MM-dd-kk-mm-ss", new Date( row.getLong(row.getColumnIndex("date_time")) )) + "~" + "0~~~~"+row.getString(row.getColumnIndex("speed"));
BW.write(line);
BW.newLine();
}
}catch (Exception e){
SendMsg("ERROR", "Can't write in file (" + e.getLocalizedMessage() + ")");
try{ftp.quit();}catch(Exception E){}
try{BW.close();}catch(Exception E){}
return;
}finally{
row.close();
try{BW.close();}catch(Exception E){}
}
packzip文件的代码
public class ZipUtil {
/**
* A constants for buffer size used to read/write data
*/
private static final int BUFFER_SIZE = 4096;
/**
* Compresses a collection of files to a destination zip file
* @param listFiles A collection of files and directories
* @param destZipFile The path of the destination zip file
* @throws FileNotFoundException
* @throws IOException
*/
public void compressFiles(List<File> listFiles, String destZipFile) throws FileNotFoundException, IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destZipFile));
for (File file : listFiles) {
if (file.isDirectory()) {
addFolderToZip(file, file.getName(), zos);
} else {
addFileToZip(file, zos);
}
}
zos.flush();
zos.close();
}
/**
* Adds a directory to the current zip output stream
* @param folder the directory to be added
* @param parentFolder the path of parent directory
* @param zos the current zip output stream
* @throws FileNotFoundException
* @throws IOException
*/
private void addFolderToZip(File folder, String parentFolder,
ZipOutputStream zos) throws FileNotFoundException, IOException {
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
addFolderToZip(file, parentFolder + "/" + file.getName(), zos);
continue;
}
zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
long bytesRead = 0;
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = bis.read(bytesIn)) != -1) {
zos.write(bytesIn, 0, read);
bytesRead += read;
}
zos.closeEntry();
}
}
/**
* Adds a file to the current zip output stream
* @param file the file to be added
* @param zos the current zip output stream
* @throws FileNotFoundException
* @throws IOException
*/
private void addFileToZip(File file, ZipOutputStream zos)
throws FileNotFoundException, IOException {
zos.putNextEntry(new ZipEntry(file.getName()));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
long bytesRead = 0;
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = bis.read(bytesIn)) != -1) {
zos.write(bytesIn, 0, read);
bytesRead += read;
}
zos.closeEntry();
}
}
范例
try {
ZipUtil zipper = new ZipUtil();
File directoryToZip = new File(path+"locations.lft");
String zipFilePath = path + "1.zip";
List<File> listFiles = new ArrayList<File>(1);
listFiles.add(directoryToZip);
zipper.compressFiles(listFiles, zipFilePath);
}catch(Exception e){
e.printStackTrace();
}
file = new File(path, "1.zip");
试试看{
ZipUtil拉链=新ZipUtil();
File directoryToZip=新文件(路径+“locations.lft”);
字符串zipFilePath=path+“1.zip”;
List listFiles=new ArrayList(1);
添加(directoryToZip);
压缩文件(listFiles,zipFilePath);
}捕获(例外e){
e、 printStackTrace();
}
file=新文件(路径“1.zip”);
很抱歉我的英语不好,希望您能了解我为什么会出现此错误请在通过ftp发送之前尝试解压缩文件。这将有助于确定这是一个压缩问题还是ftp问题。我甚至没有想过!在sendind之前,我可以毫无错误地解包,但问题仍然存在,有什么想法吗?然后错误在ftp上。正在尝试调试同一代码的示例文件。感谢subirkumarsao,问题出现在我的Ftp库中,现在它已经解决了!再次感谢!干杯自我调试万岁。自助是最好的帮助。:)
try {
ZipUtil zipper = new ZipUtil();
File directoryToZip = new File(path+"locations.lft");
String zipFilePath = path + "1.zip";
List<File> listFiles = new ArrayList<File>(1);
listFiles.add(directoryToZip);
zipper.compressFiles(listFiles, zipFilePath);
}catch(Exception e){
e.printStackTrace();
}
file = new File(path, "1.zip");